Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 1983877

Browse files
authored
feat: add rust solution to lc problem: No.1559 (doocs#1159)
1 parent 9836b5d commit 1983877

File tree

3 files changed

+178
-0
lines changed

3 files changed

+178
-0
lines changed

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

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,67 @@ public:
229229
};
230230
```
231231
232+
### **Rust**
233+
234+
```rust
235+
impl Solution {
236+
#[allow(dead_code)]
237+
pub fn contains_cycle(grid: Vec<Vec<char>>) -> bool {
238+
let n = grid.len();
239+
let m = grid[0].len();
240+
let mut d_set: Vec<usize> = vec![0; n * m];
241+
242+
// Initialize the disjoint set
243+
for i in 0..n * m {
244+
d_set[i] = i;
245+
}
246+
247+
// Traverse the grid
248+
for i in 0..n {
249+
for j in 0..m {
250+
if i + 1 < n && grid[i + 1][j] == grid[i][j] {
251+
// Check the below cell
252+
let p_curr = Self::find(i * m + j, &mut d_set);
253+
let p_below = Self::find((i + 1) * m + j, &mut d_set);
254+
if p_curr == p_below {
255+
return true;
256+
}
257+
// Otherwise, union the two cells
258+
Self::union(p_curr, p_below, &mut d_set);
259+
}
260+
// Same to the right cell
261+
if j + 1 < m && grid[i][j + 1] == grid[i][j] {
262+
let p_curr = Self::find(i * m + j, &mut d_set);
263+
let p_right = Self::find(i * m + (j + 1), &mut d_set);
264+
if p_curr == p_right {
265+
return true;
266+
}
267+
// Otherwise, union the two cells
268+
Self::union(p_curr, p_right, &mut d_set);
269+
}
270+
}
271+
}
272+
273+
false
274+
}
275+
276+
#[allow(dead_code)]
277+
fn find(x: usize, d_set: &mut Vec<usize>) -> usize {
278+
if d_set[x] != x {
279+
d_set[x] = Self::find(d_set[x], d_set);
280+
}
281+
d_set[x]
282+
}
283+
284+
#[allow(dead_code)]
285+
fn union(x: usize, y: usize, d_set: &mut Vec<usize>) {
286+
let p_x = Self::find(x, d_set);
287+
let p_y = Self::find(y, d_set);
288+
d_set[p_x] = p_y;
289+
}
290+
}
291+
```
292+
232293
### **Go**
233294

234295
```go

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

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,67 @@ public:
156156
};
157157
```
158158
159+
### **Rust**
160+
161+
```rust
162+
impl Solution {
163+
#[allow(dead_code)]
164+
pub fn contains_cycle(grid: Vec<Vec<char>>) -> bool {
165+
let n = grid.len();
166+
let m = grid[0].len();
167+
let mut d_set: Vec<usize> = vec![0; n * m];
168+
169+
// Initialize the disjoint set
170+
for i in 0..n * m {
171+
d_set[i] = i;
172+
}
173+
174+
// Traverse the grid
175+
for i in 0..n {
176+
for j in 0..m {
177+
if i + 1 < n && grid[i + 1][j] == grid[i][j] {
178+
// Check the below cell
179+
let p_curr = Self::find(i * m + j, &mut d_set);
180+
let p_below = Self::find((i + 1) * m + j, &mut d_set);
181+
if p_curr == p_below {
182+
return true;
183+
}
184+
// Otherwise, union the two cells
185+
Self::union(p_curr, p_below, &mut d_set);
186+
}
187+
// Same to the right cell
188+
if j + 1 < m && grid[i][j + 1] == grid[i][j] {
189+
let p_curr = Self::find(i * m + j, &mut d_set);
190+
let p_right = Self::find(i * m + (j + 1), &mut d_set);
191+
if p_curr == p_right {
192+
return true;
193+
}
194+
// Otherwise, union the two cells
195+
Self::union(p_curr, p_right, &mut d_set);
196+
}
197+
}
198+
}
199+
200+
false
201+
}
202+
203+
#[allow(dead_code)]
204+
fn find(x: usize, d_set: &mut Vec<usize>) -> usize {
205+
if d_set[x] != x {
206+
d_set[x] = Self::find(d_set[x], d_set);
207+
}
208+
d_set[x]
209+
}
210+
211+
#[allow(dead_code)]
212+
fn union(x: usize, y: usize, d_set: &mut Vec<usize>) {
213+
let p_x = Self::find(x, d_set);
214+
let p_y = Self::find(y, d_set);
215+
d_set[p_x] = p_y;
216+
}
217+
}
218+
```
219+
159220
### **Go**
160221

161222
```go
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
impl Solution {
2+
#[allow(dead_code)]
3+
pub fn contains_cycle(grid: Vec<Vec<char>>) -> bool {
4+
let n = grid.len();
5+
let m = grid[0].len();
6+
let mut d_set: Vec<usize> = vec![0; n * m];
7+
8+
// Initialize the disjoint set
9+
for i in 0..n * m {
10+
d_set[i] = i;
11+
}
12+
13+
// Traverse the grid
14+
for i in 0..n {
15+
for j in 0..m {
16+
if i + 1 < n && grid[i + 1][j] == grid[i][j] {
17+
// Check the below cell
18+
let p_curr = Self::find(i * m + j, &mut d_set);
19+
let p_below = Self::find((i + 1) * m + j, &mut d_set);
20+
if p_curr == p_below {
21+
return true;
22+
}
23+
// Otherwise, union the two cells
24+
Self::union(p_curr, p_below, &mut d_set);
25+
}
26+
// Same to the right cell
27+
if j + 1 < m && grid[i][j + 1] == grid[i][j] {
28+
let p_curr = Self::find(i * m + j, &mut d_set);
29+
let p_right = Self::find(i * m + (j + 1), &mut d_set);
30+
if p_curr == p_right {
31+
return true;
32+
}
33+
// Otherwise, union the two cells
34+
Self::union(p_curr, p_right, &mut d_set);
35+
}
36+
}
37+
}
38+
39+
false
40+
}
41+
42+
#[allow(dead_code)]
43+
fn find(x: usize, d_set: &mut Vec<usize>) -> usize {
44+
if d_set[x] != x {
45+
d_set[x] = Self::find(d_set[x], d_set);
46+
}
47+
d_set[x]
48+
}
49+
50+
#[allow(dead_code)]
51+
fn union(x: usize, y: usize, d_set: &mut Vec<usize>) {
52+
let p_x = Self::find(x, d_set);
53+
let p_y = Self::find(y, d_set);
54+
d_set[p_x] = p_y;
55+
}
56+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /