Skip to content

Commit 4ef98e5

Browse files
committed
959 wrong answer
1 parent d696e8c commit 4ef98e5

File tree

9 files changed

+242
-3
lines changed

9 files changed

+242
-3
lines changed
1 KB
Loading
413 Bytes
Loading
1.44 KB
Loading
743 Bytes
Loading
552 Bytes
Loading
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
# [959. Regions Cut By Slashes](https://leetcode.com/problems/regions-cut-by-slashes/)
2+
3+
In a N x N grid composed of 1 x 1 squares, each 1 x 1 square consists of a /, \, or blank space. These characters divide the square into contiguous regions.
4+
5+
(Note that backslash characters are escaped, so a \ is represented as "\\".)
6+
7+
Return the number of regions.
8+
9+
Example 1:
10+
11+
```text
12+
Input:
13+
[
14+
" /",
15+
"/ "
16+
]
17+
18+
Output: 2
19+
Explanation: The 2x2 grid is as follows:
20+
```
21+
22+
![1](1.png)
23+
24+
Example 2:
25+
26+
```text
27+
Input:
28+
[
29+
" /",
30+
" "
31+
]
32+
Output: 1
33+
Explanation: The 2x2 grid is as follows:
34+
```
35+
36+
![2](2.png)
37+
38+
Example 3:
39+
40+
```text
41+
Input:
42+
[
43+
"\\/",
44+
"/\\"
45+
]
46+
Output: 4
47+
Explanation: (Recall that because \ characters are escaped, "\\/" refers to \/, and "/\\" refers to /\.)
48+
The 2x2 grid is as follows:
49+
```
50+
51+
![3](3.png)
52+
53+
Example 4:
54+
55+
```text
56+
Input:
57+
[
58+
"/\\",
59+
"\\/"
60+
]
61+
Output: 5
62+
Explanation: (Recall that because \ characters are escaped, "/\\" refers to /\, and "\\/" refers to \/.)
63+
The 2x2 grid is as follows:
64+
```
65+
66+
![4](4.png)
67+
68+
Example 5:
69+
70+
```text
71+
Input:
72+
[
73+
"//",
74+
"/ "
75+
]
76+
Output: 3
77+
Explanation: The 2x2 grid is as follows:
78+
```
79+
80+
![5](5.png)
81+
82+
Note:
83+
84+
1. 1 <= grid.length == grid[0].length <= 30
85+
1. grid[i][j] is either '/', '\', or ' '.
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
package problem0959
2+
3+
func regionsBySlashes(grid []string) int {
4+
m := len(grid)
5+
size := m * m * 4
6+
u := newUnion(size)
7+
8+
// cut every square to 4 parts, and mark them
9+
// \0/
10+
// 3 X 1
11+
// /2\
12+
for i := 0; i < m; i++ {
13+
for j := 0; j < m; j++ {
14+
index := (i*m + j) * 4
15+
top := index + 0
16+
right := index + 1
17+
down := index + 2
18+
left := index + 3
19+
switch grid[i][j] {
20+
case '\\':
21+
u.union(top, right)
22+
u.union(down, left)
23+
case '/':
24+
u.union(top, left)
25+
u.union(down, right)
26+
default:
27+
u.union(top, right)
28+
u.union(right, down)
29+
u.union(down, left)
30+
}
31+
// union right square' left
32+
if rsl := index + 4 + 3; rsl < size {
33+
u.union(right, rsl)
34+
}
35+
// union down square' top
36+
if dst := index + 4*m; dst < size {
37+
u.union(down, dst)
38+
}
39+
}
40+
}
41+
42+
return u.count
43+
}
44+
45+
type union struct {
46+
parent []int
47+
count int
48+
}
49+
50+
func newUnion(size int) *union {
51+
parent := make([]int, size)
52+
for i := range parent {
53+
parent[i] = i
54+
}
55+
return &union{
56+
parent: parent,
57+
count: size,
58+
}
59+
}
60+
61+
func (u *union) find(i int) int {
62+
if u.parent[i] == i {
63+
return i
64+
}
65+
u.parent[i] = u.find(u.parent[i])
66+
return u.parent[i]
67+
}
68+
69+
func (u *union) union(x, y int) {
70+
xp, yp := u.find(x), u.find(y)
71+
if xp != yp {
72+
u.parent[yp] = xp
73+
u.count--
74+
}
75+
}
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
package problem0959
2+
3+
import (
4+
"testing"
5+
6+
"github.com/stretchr/testify/assert"
7+
)
8+
9+
// tcs is testcase slice
10+
var tcs = []struct {
11+
grid []string
12+
ans int
13+
}{
14+
15+
{
16+
[]string{
17+
" /",
18+
"/ ",
19+
},
20+
2,
21+
},
22+
23+
{
24+
[]string{
25+
"/",
26+
},
27+
2,
28+
},
29+
30+
{
31+
[]string{
32+
" /",
33+
" ",
34+
},
35+
1,
36+
},
37+
38+
// {
39+
// []string{
40+
// "\\/",
41+
// "/\\",
42+
// },
43+
// 4,
44+
// },
45+
46+
// {
47+
// []string{
48+
// "/\\",
49+
// "\\/",
50+
// },
51+
// 5,
52+
// },
53+
54+
// {
55+
// []string{
56+
// "//",
57+
// "/ ",
58+
// },
59+
// 3,
60+
// },
61+
62+
// // 可以有多个 testcase
63+
}
64+
65+
func Test_regionsBySlashes(t *testing.T) {
66+
ast := assert.New(t)
67+
68+
for _, tc := range tcs {
69+
ast.Equal(tc.ans, regionsBySlashes(tc.grid), "输入:%v", tc)
70+
}
71+
}
72+
73+
func Benchmark_regionsBySlashes(b *testing.B) {
74+
for i := 0; i < b.N; i++ {
75+
for _, tc := range tcs {
76+
regionsBySlashes(tc.grid)
77+
}
78+
}
79+
}

leetcode.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"Username": "aQuaYi",
33
"Ranking": 902,
4-
"Updated": "2019-03-23T14:53:21.896823987+08:00",
4+
"Updated": "2019-03-23T15:20:36.739536901+08:00",
55
"Record": {
66
"Easy": {
77
"Solved": 215,
@@ -3025,7 +3025,7 @@
30253025
"ID": 250,
30263026
"Title": "Count Univalue Subtrees",
30273027
"TitleSlug": "count-univalue-subtrees",
3028-
"PassRate": "47%",
3028+
"PassRate": "48%",
30293029
"Difficulty": "Medium",
30303030
"IsAccepted": false,
30313031
"IsPaid": true,
@@ -11965,7 +11965,7 @@
1196511965
"ID": 995,
1196611966
"Title": "Minimum Number of K Consecutive Bit Flips",
1196711967
"TitleSlug": "minimum-number-of-k-consecutive-bit-flips",
11968-
"PassRate": "50%",
11968+
"PassRate": "51%",
1196911969
"Difficulty": "Hard",
1197011970
"IsAccepted": false,
1197111971
"IsPaid": false,

0 commit comments

Comments
 (0)