Skip to content

Commit 7f1dd0f

Browse files
committed
feat: update solutions to lc problem: No.0959
No.0959.Regions Cut By Slashes
1 parent 46bbbb4 commit 7f1dd0f

File tree

6 files changed

+286
-406
lines changed

6 files changed

+286
-406
lines changed

solution/0900-0999/0959.Regions Cut By Slashes/README.md

Lines changed: 90 additions & 108 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,11 @@
66

77
<!-- 这里写题目描述 -->
88

9-
<p>在由 1 x 1 方格组成的 N x N 网格&nbsp;<code>grid</code> 中,每个 1 x 1&nbsp;方块由 <code>/</code>、<code>\</code> 或空格构成。这些字符会将方块划分为一些共边的区域。</p>
9+
<p>在由 <code>1 x 1</code> 方格组成的 <code>n&nbsp;x n</code>&nbsp;网格&nbsp;<code>grid</code> 中,每个 <code>1 x 1</code>&nbsp;方块由 <code>'/'</code>、<code>'\'</code> 或空格构成。这些字符会将方块划分为一些共边的区域。</p>
1010

11-
<p>(请注意,反斜杠字符是转义的,因此 <code>\</code> 用 <code>&quot;\\&quot;</code>&nbsp;表示。)。</p>
11+
<p>给定网格&nbsp;<code>grid</code>&nbsp;表示为一个字符串数组,返回 <em>区域的数量</em> 。</p>
1212

13-
<p>返回区域的数目。</p>
13+
<p>请注意,反斜杠字符是转义的,因此&nbsp;<code>'\'</code> 用 <code>'\\'</code>&nbsp;表示。</p>
1414

1515
<p>&nbsp;</p>
1616

@@ -19,78 +19,50 @@
1919

2020
<p><strong>示例 1:</strong></p>
2121

22-
<pre><strong>输入:
23-
</strong>[
24-
&nbsp; &quot; /&quot;,
25-
&nbsp; &quot;/ &quot;
26-
]
27-
<strong>输出:</strong>2
28-
<strong>解释:</strong>2x2 网格如下:
29-
<img alt="" src="https://cdn.jsdelivr.net/gh/doocs/leetcode@main/solution/0900-0999/0959.Regions%20Cut%20By%20Slashes/images/1.png"></pre>
22+
<p><img src="https://cdn.jsdelivr.net/gh/doocs/leetcode@main/solution/0900-0999/0959.Regions%20Cut%20By%20Slashes/images/1.png" style="height: 200px; width: 200px;" /></p>
23+
24+
<pre>
25+
<strong>输入:</strong>grid = [" /","/ "]
26+
<strong>输出:</strong>2</pre>
3027

3128
<p><strong>示例 2:</strong></p>
3229

33-
<pre><strong>输入:
34-
</strong>[
35-
&nbsp; &quot; /&quot;,
36-
&nbsp; &quot; &quot;
37-
]
30+
<p><img src="https://cdn.jsdelivr.net/gh/doocs/leetcode@main/solution/0900-0999/0959.Regions%20Cut%20By%20Slashes/images/2.png" style="height: 198px; width: 200px;" /></p>
31+
32+
<pre>
33+
<strong>输入:</strong>grid = [" /"," "]
3834
<strong>输出:</strong>1
39-
<strong>解释:</strong>2x2 网格如下:
40-
<img alt="" src="https://cdn.jsdelivr.net/gh/doocs/leetcode@main/solution/0900-0999/0959.Regions%20Cut%20By%20Slashes/images/2.png"></pre>
35+
</pre>
4136

4237
<p><strong>示例 3:</strong></p>
4338

44-
<pre><strong>输入:
45-
</strong>[
46-
&nbsp; &quot;\\/&quot;,
47-
&nbsp; &quot;/\\&quot;
48-
]
49-
<strong>输出:</strong>4
50-
<strong>解释:</strong>(回想一下,因为 \ 字符是转义的,所以 &quot;\\/&quot; 表示 \/,而 &quot;/\\&quot; 表示 /\。)
51-
2x2 网格如下:
52-
<img alt="" src="https://cdn.jsdelivr.net/gh/doocs/leetcode@main/solution/0900-0999/0959.Regions%20Cut%20By%20Slashes/images/3.png"></pre>
53-
54-
<p><strong>示例 4:</strong></p>
55-
56-
<pre><strong>输入:
57-
</strong>[
58-
&nbsp; &quot;/\\&quot;,
59-
&nbsp; &quot;\\/&quot;
60-
]
39+
<p><img src="https://cdn.jsdelivr.net/gh/doocs/leetcode@main/solution/0900-0999/0959.Regions%20Cut%20By%20Slashes/images/4.png" style="height: 200px; width: 200px;" /></p>
40+
41+
<pre>
42+
<strong>输入:</strong>grid = ["/\\","\\/"]
6143
<strong>输出:</strong>5
62-
<strong>解释:</strong>(回想一下,因为 \ 字符是转义的,所以 &quot;/\\&quot; 表示 /\,而 &quot;\\/&quot; 表示 \/。)
63-
2x2 网格如下:
64-
<img alt="" src="https://cdn.jsdelivr.net/gh/doocs/leetcode@main/solution/0900-0999/0959.Regions%20Cut%20By%20Slashes/images/4.png"></pre>
65-
66-
<p><strong>示例 5:</strong></p>
67-
68-
<pre><strong>输入:
69-
</strong>[
70-
&nbsp; &quot;//&quot;,
71-
&nbsp; &quot;/ &quot;
72-
]
73-
<strong>输出:</strong>3
74-
<strong>解释:</strong>2x2 网格如下:
75-
<img alt="" src="https://cdn.jsdelivr.net/gh/doocs/leetcode@main/solution/0900-0999/0959.Regions%20Cut%20By%20Slashes/images/5.png">
44+
<strong>解释:</strong>回想一下,因为 \ 字符是转义的,所以 "/\\" 表示 /\,而 "\\/" 表示 \/。
7645
</pre>
7746

7847
<p>&nbsp;</p>
7948

8049
<p><strong>提示:</strong></p>
8150

82-
<ol>
83-
<li><code>1 &lt;= grid.length == grid[0].length &lt;= 30</code></li>
84-
<li><code>grid[i][j]</code> 是&nbsp;<code>&#39;/&#39;</code>、<code>&#39;\&#39;</code>、或&nbsp;<code>&#39; &#39;</code>。</li>
85-
</ol>
51+
<ul>
52+
<li><code>n == grid.length == grid[i].length</code></li>
53+
<li><code>1 &lt;= n &lt;= 30</code></li>
54+
<li><code>grid[i][j]</code> 是&nbsp;<code>'/'</code>、<code>'\'</code>、或&nbsp;<code>' '</code></li>
55+
</ul>
56+
57+
8658

8759
## 解法
8860

8961
<!-- 这里可写通用的实现逻辑 -->
9062

91-
并查集。
63+
并查集。对于本题,可以把每个方块看成四个三角形,从上开始顺时针编号 0,1,2,3,`'/'`代表 0 和 3,1 和 2 连通,`'\\'` 代表 0 和 1,2 和 3 连通,`' '` 代表 0、1、2、3 都联通,然后再和方块周围的三角形联通,最后返回总的连通分量就得到结果了。
9264

93-
并查集模板:
65+
以下是并查集的几个常用模板。
9466

9567
模板 1——朴素并查集:
9668

@@ -149,8 +121,6 @@ p[find(a)] = find(b)
149121
d[find(a)] = distance
150122
```
151123

152-
对于本题,可以把每个方块看成四个三角形,从上开始顺时针编号 0,1,2,3,`'/'`代表 0、3,1、2 连通,`'\\'` 代表 0、1,2、3 连通,`' '` 代表 0、1、2、3 都联通,然后再和方块周围的三角形联通,最后返回总的连通分量就得到结果了。
153-
154124
<!-- tabs:start -->
155125

156126
### **Python3**
@@ -252,38 +222,48 @@ class Solution {
252222
class Solution {
253223
public:
254224
vector<int> p;
225+
int size;
255226

256227
int regionsBySlashes(vector<string>& grid) {
257228
int n = grid.size();
258-
for (int i = 0; i < n * n * 4; ++i) p.push_back(i);
259-
for (int i = 0; i < n; ++i) {
260-
string row = grid[i];
261-
for (int j = 0; j < n; ++j) {
262-
int idx = i * n + j;
263-
if (i < n - 1) p[find(idx * 4 + 2)] = find((idx + n) * 4);
264-
if (j < n - 1) p[find(idx * 4 + 1)] = find((idx + 1) * 4 + 3);
265-
if (row[j] == '/')
229+
size = n * n * 4;
230+
p.resize(size);
231+
for (int i = 0; i < size; ++i) p[i] = i;
232+
for (int i = 0; i < n; ++i)
233+
{
234+
for (int j = 0; j < n; ++j)
235+
{
236+
int k = i * n + j;
237+
if (i < n - 1) merge(4 * k + 2, (k + n) * 4);
238+
if (j < n - 1) merge(4 * k + 1, (k + 1) * 4 + 3);
239+
char v = grid[i][j];
240+
if (v == '/')
266241
{
267-
p[find(idx * 4)] = find(idx * 4 + 3);
268-
p[find(idx * 4 + 1)] = find(idx * 4 + 2);
242+
merge(4 * k, 4 * k + 3);
243+
merge(4 * k + 1, 4 * k + 2);
269244
}
270-
else if (row[j] == '\\')
245+
else if (v == '\\')
271246
{
272-
p[find(idx * 4)] = find(idx * 4 + 1);
273-
p[find(idx * 4 + 2)] = find(idx * 4 + 3);
247+
merge(4 * k, 4 * k + 1);
248+
merge(4 * k + 2, 4 * k + 3);
274249
}
275250
else
276251
{
277-
p[find(idx * 4)] = find(idx * 4 + 1);
278-
p[find(idx * 4 + 1)] = find(idx * 4 + 2);
279-
p[find(idx * 4 + 2)] = find(idx * 4 + 3);
252+
merge(4 * k, 4 * k + 1);
253+
merge(4 * k + 1, 4 * k + 2);
254+
merge(4 * k + 2, 4 * k + 3);
280255
}
281256
}
282257
}
283-
unordered_set<int> s;
284-
for (int i = 0; i < p.size(); ++i)
285-
s.insert(find(i));
286-
return s.size();
258+
return size;
259+
}
260+
261+
void merge(int a, int b) {
262+
int pa = find(a);
263+
int pb = find(b);
264+
if (pa == pb) return;
265+
p[pa] = pb;
266+
--size;
287267
}
288268

289269
int find(int x) {
@@ -296,49 +276,51 @@ public:
296276
### **Go**
297277

298278
```go
299-
var p []int
300-
301279
func regionsBySlashes(grid []string) int {
302280
n := len(grid)
303-
p = make([]int, n*n*4)
304-
for i := 0; i < len(p); i++ {
281+
size := n * n * 4
282+
p := make([]int, size)
283+
for i := range p {
305284
p[i] = i
306285
}
307-
for i := 0; i < n; i++ {
308-
row := grid[i]
309-
for j := 0; j < n; j++ {
310-
idx := i*n + j
286+
var find func(x int) int
287+
find = func(x int) int {
288+
if p[x] != x {
289+
p[x] = find(p[x])
290+
}
291+
return p[x]
292+
}
293+
union := func(a, b int) {
294+
pa, pb := find(a), find(b)
295+
if pa == pb {
296+
return
297+
}
298+
p[pa] = pb
299+
size--
300+
}
301+
for i, row := range grid {
302+
for j, v := range row {
303+
k := i*n + j
311304
if i < n-1 {
312-
p[find(idx*4+2)] = find((idx + n) * 4)
305+
union(4*k+2, (k+n)*4)
313306
}
314307
if j < n-1 {
315-
p[find(idx*4+1)] = find((idx+1)*4 + 3)
308+
union(4*k+1, (k+1)*4+3)
316309
}
317-
if row[j] == '/' {
318-
p[find(idx*4)] = find(idx*4 + 3)
319-
p[find(idx*4+1)] = find(idx*4 + 2)
320-
} else if row[j] == '\\' {
321-
p[find(idx*4)] = find(idx*4 + 1)
322-
p[find(idx*4+2)] = find(idx*4 + 3)
310+
if v == '/' {
311+
union(4*k, 4*k+3)
312+
union(4*k+1, 4*k+2)
313+
} else if v == '\\' {
314+
union(4*k, 4*k+1)
315+
union(4*k+2, 4*k+3)
323316
} else {
324-
p[find(idx*4)] = find(idx*4 + 1)
325-
p[find(idx*4+1)] = find(idx*4 + 2)
326-
p[find(idx*4+2)] = find(idx*4 + 3)
317+
union(4*k, 4*k+1)
318+
union(4*k+1, 4*k+2)
319+
union(4*k+2, 4*k+3)
327320
}
328321
}
329322
}
330-
s := make(map[int]bool)
331-
for i := 0; i < len(p); i++ {
332-
s[find(i)] = true
333-
}
334-
return len(s)
335-
}
336-
337-
func find(x int) int {
338-
if p[x] != x {
339-
p[x] = find(p[x])
340-
}
341-
return p[x]
323+
return size
342324
}
343325
```
344326

0 commit comments

Comments
 (0)