Skip to content

Commit a01b98c

Browse files
committed
feat: add solutions to lc problem: No.1559
No.1559.Detect Cycles in 2D Grid
1 parent 8dd5cb6 commit a01b98c

File tree

15 files changed

+610
-33
lines changed

15 files changed

+610
-33
lines changed

solution/0500-0599/0553.Optimal Division/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@
4343

4444
要使得除法的结果最大,分子应该尽可能大,而分母应该尽可能小。
4545

46-
分子最大应该是 nums[0],而分母最大是 `nums[1] / nums[2] / ... / nums[n - 1]`,此时的除法结果最大。
46+
分子最大应该是 `nums[0]`,而分母最大是 `nums[1] / nums[2] / ... / nums[n - 1]`,此时的除法结果最大。
4747

4848
<!-- tabs:start -->
4949

solution/1500-1599/1559.Detect Cycles in 2D Grid/README.md

Lines changed: 184 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,22 +60,205 @@
6060

6161
<!-- 这里可写通用的实现逻辑 -->
6262

63+
构造并查集,遍历每个坐标 `(i, j)`,如果下方或者右侧的元素 `(x, y)` 与当前元素 `(i, j)` 相同,进行合并操作。若是,若此前两个坐标已经处于连通状态,再进行合并时会形成环,直接返回 true。否则遍历结束返回 false。
64+
65+
并查集模板:
66+
67+
模板 1——朴素并查集:
68+
69+
```python
70+
# 初始化,p存储每个点的父节点
71+
p = list(range(n))
72+
73+
# 返回x的祖宗节点
74+
def find(x):
75+
if p[x] != x:
76+
# 路径压缩
77+
p[x] = find(p[x])
78+
return p[x]
79+
80+
# 合并a和b所在的两个集合
81+
p[find(a)] = find(b)
82+
```
83+
84+
模板 2——维护 size 的并查集:
85+
86+
```python
87+
# 初始化,p存储每个点的父节点,size只有当节点是祖宗节点时才有意义,表示祖宗节点所在集合中,点的数量
88+
p = list(range(n))
89+
size = [1] * n
90+
91+
# 返回x的祖宗节点
92+
def find(x):
93+
if p[x] != x:
94+
# 路径压缩
95+
p[x] = find(p[x])
96+
return p[x]
97+
98+
# 合并a和b所在的两个集合
99+
if find(a) != find(b):
100+
size[find(b)] += size[find(a)]
101+
p[find(a)] = find(b)
102+
```
103+
104+
模板 3——维护到祖宗节点距离的并查集:
105+
106+
```python
107+
# 初始化,p存储每个点的父节点,d[x]存储x到p[x]的距离
108+
p = list(range(n))
109+
d = [0] * n
110+
111+
# 返回x的祖宗节点
112+
def find(x):
113+
if p[x] != x:
114+
t = find(p[x])
115+
d[x] += d[p[x]]
116+
p[x] = t
117+
return p[x]
118+
119+
# 合并a和b所在的两个集合
120+
p[find(a)] = find(b)
121+
d[find(a)] = distance
122+
```
123+
63124
<!-- tabs:start -->
64125

65126
### **Python3**
66127

67128
<!-- 这里可写当前语言的特殊实现逻辑 -->
68129

69130
```python
70-
131+
class Solution:
132+
def containsCycle(self, grid: List[List[str]]) -> bool:
133+
def find(x):
134+
if p[x] != x:
135+
p[x] = find(p[x])
136+
return p[x]
137+
138+
m, n = len(grid), len(grid[0])
139+
p = list(range(m * n))
140+
for i in range(m):
141+
for j in range(n):
142+
for a, b in [[0, 1], [1, 0]]:
143+
x, y = i + a, j + b
144+
if 0 <= x < m and 0 <= y < n and grid[x][y] == grid[i][j]:
145+
if find(x * n + y) == find(i * n + j):
146+
return True
147+
p[find(x * n + y)] = find(i * n + j)
148+
return False
71149
```
72150

73151
### **Java**
74152

75153
<!-- 这里可写当前语言的特殊实现逻辑 -->
76154

77155
```java
156+
class Solution {
157+
private int[] p;
158+
159+
public boolean containsCycle(char[][] grid) {
160+
int m = grid.length;
161+
int n = grid[0].length;
162+
p = new int[m * n];
163+
for (int i = 0; i < p.length; ++i) {
164+
p[i] = i;
165+
}
166+
int[] dirs = {0, 1, 0};
167+
for (int i = 0; i < m; ++i) {
168+
for (int j = 0; j < n; ++j) {
169+
for (int k = 0; k < 2; ++k) {
170+
int x = i + dirs[k];
171+
int y = j + dirs[k + 1];
172+
if (x >= 0 && x < m && y >= 0 && y < n && grid[i][j] == grid[x][y]) {
173+
if (find(x * n + y) == find(i * n + j)) {
174+
return true;
175+
}
176+
p[find(x * n + y)] = find(i * n + j);
177+
}
178+
}
179+
}
180+
}
181+
return false;
182+
}
183+
184+
private int find(int x) {
185+
if (p[x] != x) {
186+
p[x] = find(p[x]);
187+
}
188+
return p[x];
189+
}
190+
}
191+
```
192+
193+
### **C++**
194+
195+
```cpp
196+
class Solution {
197+
public:
198+
vector<int> p;
199+
200+
bool containsCycle(vector<vector<char>>& grid) {
201+
int m = grid.size(), n = grid[0].size();
202+
p.resize(m * n);
203+
for (int i = 0; i < p.size(); ++i) p[i] = i;
204+
vector<int> dirs = {0, 1, 0};
205+
for (int i = 0; i < m; ++i)
206+
{
207+
for (int j = 0; j < n; ++j)
208+
{
209+
for (int k = 0; k < 2; ++k)
210+
{
211+
int x = i + dirs[k], y = j + dirs[k + 1];
212+
if (x >= 0 && x < m && y >= 0 && y < n && grid[x][y] == grid[i][j])
213+
{
214+
if (find(x * n + y) == find(i * n + j)) return 1;
215+
p[find(x * n + y)] = find(i * n + j);
216+
}
217+
}
218+
}
219+
}
220+
return 0;
221+
}
222+
223+
int find(int x) {
224+
if (p[x] != x) p[x] = find(p[x]);
225+
return p[x];
226+
}
227+
};
228+
```
78229
230+
### **Go**
231+
232+
```go
233+
func containsCycle(grid [][]byte) bool {
234+
m, n := len(grid), len(grid[0])
235+
p := make([]int, m*n)
236+
for i := range p {
237+
p[i] = i
238+
}
239+
var find func(x int) int
240+
find = func(x int) int {
241+
if p[x] != x {
242+
p[x] = find(p[x])
243+
}
244+
return p[x]
245+
}
246+
dirs := []int{1, 0, 1}
247+
for i := 0; i < m; i++ {
248+
for j := 0; j < n; j++ {
249+
for k := 0; k < 2; k++ {
250+
x, y := i+dirs[k], j+dirs[k+1]
251+
if x >= 0 && x < m && y >= 0 && y < n && grid[x][y] == grid[i][j] {
252+
if find(x*n+y) == find(i*n+j) {
253+
return true
254+
}
255+
p[find(x*n+y)] = find(i*n + j)
256+
}
257+
}
258+
}
259+
}
260+
return false
261+
}
79262
```
80263

81264
### **...**

solution/1500-1599/1559.Detect Cycles in 2D Grid/README_EN.md

Lines changed: 125 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,18 +57,142 @@
5757

5858
## Solutions
5959

60+
Union find.
61+
6062
<!-- tabs:start -->
6163

6264
### **Python3**
6365

6466
```python
65-
67+
class Solution:
68+
def containsCycle(self, grid: List[List[str]]) -> bool:
69+
def find(x):
70+
if p[x] != x:
71+
p[x] = find(p[x])
72+
return p[x]
73+
74+
m, n = len(grid), len(grid[0])
75+
p = list(range(m * n))
76+
for i in range(m):
77+
for j in range(n):
78+
for a, b in [[0, 1], [1, 0]]:
79+
x, y = i + a, j + b
80+
if 0 <= x < m and 0 <= y < n and grid[x][y] == grid[i][j]:
81+
if find(x * n + y) == find(i * n + j):
82+
return True
83+
p[find(x * n + y)] = find(i * n + j)
84+
return False
6685
```
6786

6887
### **Java**
6988

7089
```java
90+
class Solution {
91+
private int[] p;
92+
93+
public boolean containsCycle(char[][] grid) {
94+
int m = grid.length;
95+
int n = grid[0].length;
96+
p = new int[m * n];
97+
for (int i = 0; i < p.length; ++i) {
98+
p[i] = i;
99+
}
100+
int[] dirs = {0, 1, 0};
101+
for (int i = 0; i < m; ++i) {
102+
for (int j = 0; j < n; ++j) {
103+
for (int k = 0; k < 2; ++k) {
104+
int x = i + dirs[k];
105+
int y = j + dirs[k + 1];
106+
if (x >= 0 && x < m && y >= 0 && y < n && grid[i][j] == grid[x][y]) {
107+
if (find(x * n + y) == find(i * n + j)) {
108+
return true;
109+
}
110+
p[find(x * n + y)] = find(i * n + j);
111+
}
112+
}
113+
}
114+
}
115+
return false;
116+
}
117+
118+
private int find(int x) {
119+
if (p[x] != x) {
120+
p[x] = find(p[x]);
121+
}
122+
return p[x];
123+
}
124+
}
125+
```
126+
127+
### **C++**
128+
129+
```cpp
130+
class Solution {
131+
public:
132+
vector<int> p;
133+
134+
bool containsCycle(vector<vector<char>>& grid) {
135+
int m = grid.size(), n = grid[0].size();
136+
p.resize(m * n);
137+
for (int i = 0; i < p.size(); ++i) p[i] = i;
138+
vector<int> dirs = {0, 1, 0};
139+
for (int i = 0; i < m; ++i)
140+
{
141+
for (int j = 0; j < n; ++j)
142+
{
143+
for (int k = 0; k < 2; ++k)
144+
{
145+
int x = i + dirs[k], y = j + dirs[k + 1];
146+
if (x >= 0 && x < m && y >= 0 && y < n && grid[x][y] == grid[i][j])
147+
{
148+
if (find(x * n + y) == find(i * n + j)) return 1;
149+
p[find(x * n + y)] = find(i * n + j);
150+
}
151+
}
152+
}
153+
}
154+
return 0;
155+
}
156+
157+
int find(int x) {
158+
if (p[x] != x) p[x] = find(p[x]);
159+
return p[x];
160+
}
161+
};
162+
```
71163
164+
### **Go**
165+
166+
```go
167+
func containsCycle(grid [][]byte) bool {
168+
m, n := len(grid), len(grid[0])
169+
p := make([]int, m*n)
170+
for i := range p {
171+
p[i] = i
172+
}
173+
var find func(x int) int
174+
find = func(x int) int {
175+
if p[x] != x {
176+
p[x] = find(p[x])
177+
}
178+
return p[x]
179+
}
180+
dirs := []int{1, 0, 1}
181+
for i := 0; i < m; i++ {
182+
for j := 0; j < n; j++ {
183+
for k := 0; k < 2; k++ {
184+
x, y := i+dirs[k], j+dirs[k+1]
185+
if x >= 0 && x < m && y >= 0 && y < n && grid[x][y] == grid[i][j] {
186+
if find(x*n+y) == find(i*n+j) {
187+
return true
188+
}
189+
p[find(x*n+y)] = find(i*n + j)
190+
}
191+
}
192+
}
193+
}
194+
return false
195+
}
72196
```
73197

74198
### **...**
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
class Solution {
2+
public:
3+
vector<int> p;
4+
5+
bool containsCycle(vector<vector<char>>& grid) {
6+
int m = grid.size(), n = grid[0].size();
7+
p.resize(m * n);
8+
for (int i = 0; i < p.size(); ++i) p[i] = i;
9+
vector<int> dirs = {0, 1, 0};
10+
for (int i = 0; i < m; ++i)
11+
{
12+
for (int j = 0; j < n; ++j)
13+
{
14+
for (int k = 0; k < 2; ++k)
15+
{
16+
int x = i + dirs[k], y = j + dirs[k + 1];
17+
if (x >= 0 && x < m && y >= 0 && y < n && grid[x][y] == grid[i][j])
18+
{
19+
if (find(x * n + y) == find(i * n + j)) return 1;
20+
p[find(x * n + y)] = find(i * n + j);
21+
}
22+
}
23+
}
24+
}
25+
return 0;
26+
}
27+
28+
int find(int x) {
29+
if (p[x] != x) p[x] = find(p[x]);
30+
return p[x];
31+
}
32+
};

0 commit comments

Comments
 (0)