Skip to content

Commit 9fc131f

Browse files
committed
feat: add rust solution to lc problems: No.0130,0797,1091
- No.0130.Surrounded Regions - No.0797.All Paths From Source to Target - No.1091.Shortest Path in Binary Matrix
1 parent 07f9173 commit 9fc131f

File tree

9 files changed

+309
-0
lines changed

9 files changed

+309
-0
lines changed

solution/0100-0199/0130.Surrounded Regions/README.md

+47
Original file line numberDiff line numberDiff line change
@@ -458,6 +458,53 @@ func solve(board [][]byte) {
458458
}
459459
```
460460

461+
### **Rust**
462+
463+
```rust
464+
impl Solution {
465+
fn dfs(i: usize, j: usize, mark: char, vis: &mut Vec<Vec<bool>>, board: &mut Vec<Vec<char>>) {
466+
if vis[i][j] || board[i][j] != mark {
467+
return;
468+
}
469+
vis[i][j] = true;
470+
if i > 0 {
471+
Self::dfs(i - 1, j, mark, vis, board);
472+
}
473+
if i < vis.len() - 1 {
474+
Self::dfs(i + 1, j, mark, vis, board);
475+
}
476+
if j > 0 {
477+
Self::dfs(i, j - 1, mark, vis, board);
478+
}
479+
if j < vis[0].len() - 1 {
480+
Self::dfs(i, j + 1, mark, vis, board);
481+
}
482+
}
483+
484+
pub fn solve(board: &mut Vec<Vec<char>>) {
485+
let m = board.len();
486+
let n = board[0].len();
487+
let mut vis = vec![vec![false; n]; m];
488+
for i in 0..m {
489+
Self::dfs(i, 0, board[i][0], &mut vis, board);
490+
Self::dfs(i, n - 1, board[i][n - 1], &mut vis, board);
491+
}
492+
for i in 0..n {
493+
Self::dfs(0, i, board[0][i], &mut vis, board);
494+
Self::dfs(m - 1, i, board[m - 1][i], &mut vis, board);
495+
}
496+
for i in 0..m {
497+
for j in 0..n {
498+
if vis[i][j] {
499+
continue;
500+
}
501+
board[i][j] = 'X';
502+
}
503+
}
504+
}
505+
}
506+
```
507+
461508
### **...**
462509

463510
```

solution/0100-0199/0130.Surrounded Regions/README_EN.md

+47
Original file line numberDiff line numberDiff line change
@@ -444,6 +444,53 @@ func solve(board [][]byte) {
444444
}
445445
```
446446

447+
### **Rust**
448+
449+
```rust
450+
impl Solution {
451+
fn dfs(i: usize, j: usize, mark: char, vis: &mut Vec<Vec<bool>>, board: &mut Vec<Vec<char>>) {
452+
if vis[i][j] || board[i][j] != mark {
453+
return;
454+
}
455+
vis[i][j] = true;
456+
if i > 0 {
457+
Self::dfs(i - 1, j, mark, vis, board);
458+
}
459+
if i < vis.len() - 1 {
460+
Self::dfs(i + 1, j, mark, vis, board);
461+
}
462+
if j > 0 {
463+
Self::dfs(i, j - 1, mark, vis, board);
464+
}
465+
if j < vis[0].len() - 1 {
466+
Self::dfs(i, j + 1, mark, vis, board);
467+
}
468+
}
469+
470+
pub fn solve(board: &mut Vec<Vec<char>>) {
471+
let m = board.len();
472+
let n = board[0].len();
473+
let mut vis = vec![vec![false; n]; m];
474+
for i in 0..m {
475+
Self::dfs(i, 0, board[i][0], &mut vis, board);
476+
Self::dfs(i, n - 1, board[i][n - 1], &mut vis, board);
477+
}
478+
for i in 0..n {
479+
Self::dfs(0, i, board[0][i], &mut vis, board);
480+
Self::dfs(m - 1, i, board[m - 1][i], &mut vis, board);
481+
}
482+
for i in 0..m {
483+
for j in 0..n {
484+
if vis[i][j] {
485+
continue;
486+
}
487+
board[i][j] = 'X';
488+
}
489+
}
490+
}
491+
}
492+
```
493+
447494
### **...**
448495

449496
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
impl Solution {
2+
fn dfs(i: usize, j: usize, mark: char, vis: &mut Vec<Vec<bool>>, board: &mut Vec<Vec<char>>) {
3+
if vis[i][j] || board[i][j] != mark {
4+
return;
5+
}
6+
vis[i][j] = true;
7+
if i > 0 {
8+
Self::dfs(i - 1, j, mark, vis, board);
9+
}
10+
if i < vis.len() - 1 {
11+
Self::dfs(i + 1, j, mark, vis, board);
12+
}
13+
if j > 0 {
14+
Self::dfs(i, j - 1, mark, vis, board);
15+
}
16+
if j < vis[0].len() - 1 {
17+
Self::dfs(i, j + 1, mark, vis, board);
18+
}
19+
}
20+
21+
pub fn solve(board: &mut Vec<Vec<char>>) {
22+
let m = board.len();
23+
let n = board[0].len();
24+
let mut vis = vec![vec![false; n]; m];
25+
for i in 0..m {
26+
Self::dfs(i, 0, board[i][0], &mut vis, board);
27+
Self::dfs(i, n - 1, board[i][n - 1], &mut vis, board);
28+
}
29+
for i in 0..n {
30+
Self::dfs(0, i, board[0][i], &mut vis, board);
31+
Self::dfs(m - 1, i, board[m - 1][i], &mut vis, board);
32+
}
33+
for i in 0..m {
34+
for j in 0..n {
35+
if vis[i][j] {
36+
continue;
37+
}
38+
board[i][j] = 'X';
39+
}
40+
}
41+
}
42+
}

solution/0700-0799/0797.All Paths From Source to Target/README.md

+23
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,29 @@ var allPathsSourceTarget = function (graph) {
249249
};
250250
```
251251

252+
### **Rust**
253+
254+
```rust
255+
impl Solution {
256+
fn dfs(i: usize, path: &mut Vec<i32>, res: &mut Vec<Vec<i32>>, graph: &Vec<Vec<i32>>) {
257+
path.push(i as i32);
258+
if i == graph.len() - 1 {
259+
res.push(path.clone());
260+
}
261+
for j in graph[i].iter() {
262+
Self::dfs(*j as usize, path, res, graph)
263+
}
264+
path.pop();
265+
}
266+
267+
pub fn all_paths_source_target(graph: Vec<Vec<i32>>) -> Vec<Vec<i32>> {
268+
let mut res = Vec::new();
269+
Self::dfs(0, &mut vec![], &mut res, &graph);
270+
res
271+
}
272+
}
273+
```
274+
252275
### **...**
253276

254277
```

solution/0700-0799/0797.All Paths From Source to Target/README_EN.md

+23
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,29 @@ var allPathsSourceTarget = function (graph) {
233233
};
234234
```
235235

236+
### **Rust**
237+
238+
```rust
239+
impl Solution {
240+
fn dfs(i: usize, path: &mut Vec<i32>, res: &mut Vec<Vec<i32>>, graph: &Vec<Vec<i32>>) {
241+
path.push(i as i32);
242+
if i == graph.len() - 1 {
243+
res.push(path.clone());
244+
}
245+
for j in graph[i].iter() {
246+
Self::dfs(*j as usize, path, res, graph)
247+
}
248+
path.pop();
249+
}
250+
251+
pub fn all_paths_source_target(graph: Vec<Vec<i32>>) -> Vec<Vec<i32>> {
252+
let mut res = Vec::new();
253+
Self::dfs(0, &mut vec![], &mut res, &graph);
254+
res
255+
}
256+
}
257+
```
258+
236259
### **...**
237260

238261
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
impl Solution {
2+
fn dfs(i: usize, path: &mut Vec<i32>, res: &mut Vec<Vec<i32>>, graph: &Vec<Vec<i32>>) {
3+
path.push(i as i32);
4+
if i == graph.len() - 1 {
5+
res.push(path.clone());
6+
}
7+
for j in graph[i].iter() {
8+
Self::dfs(*j as usize, path, res, graph)
9+
}
10+
path.pop();
11+
}
12+
13+
pub fn all_paths_source_target(graph: Vec<Vec<i32>>) -> Vec<Vec<i32>> {
14+
let mut res = Vec::new();
15+
Self::dfs(0, &mut vec![], &mut res, &graph);
16+
res
17+
}
18+
}

solution/1000-1099/1091.Shortest Path in Binary Matrix/README.md

+38
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,44 @@ func shortestPathBinaryMatrix(grid [][]int) int {
197197
}
198198
```
199199

200+
### **Rust**
201+
202+
```rust
203+
use std::collections::VecDeque;
204+
impl Solution {
205+
pub fn shortest_path_binary_matrix(mut grid: Vec<Vec<i32>>) -> i32 {
206+
let n = grid.len();
207+
let mut queue = VecDeque::new();
208+
queue.push_back([0, 0]);
209+
let mut res = 0;
210+
while !queue.is_empty() {
211+
res += 1;
212+
for _ in 0..queue.len() {
213+
let [i, j] = queue.pop_front().unwrap();
214+
if grid[i][j] == 1 {
215+
continue;
216+
}
217+
if i == n - 1 && j == n - 1 {
218+
return res;
219+
}
220+
grid[i][j] = 1;
221+
for x in -1..=1 {
222+
for y in -1..=1 {
223+
let x = x + i as i32;
224+
let y = y + j as i32;
225+
if x < 0 || x == n as i32 || y < 0 || y == n as i32 {
226+
continue;
227+
}
228+
queue.push_back([x as usize, y as usize]);
229+
}
230+
}
231+
}
232+
}
233+
-1
234+
}
235+
}
236+
```
237+
200238
### **...**
201239

202240
```

solution/1000-1099/1091.Shortest Path in Binary Matrix/README_EN.md

+38
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,44 @@ func shortestPathBinaryMatrix(grid [][]int) int {
187187
}
188188
```
189189

190+
### **Rust**
191+
192+
```rust
193+
use std::collections::VecDeque;
194+
impl Solution {
195+
pub fn shortest_path_binary_matrix(mut grid: Vec<Vec<i32>>) -> i32 {
196+
let n = grid.len();
197+
let mut queue = VecDeque::new();
198+
queue.push_back([0, 0]);
199+
let mut res = 0;
200+
while !queue.is_empty() {
201+
res += 1;
202+
for _ in 0..queue.len() {
203+
let [i, j] = queue.pop_front().unwrap();
204+
if grid[i][j] == 1 {
205+
continue;
206+
}
207+
if i == n - 1 && j == n - 1 {
208+
return res;
209+
}
210+
grid[i][j] = 1;
211+
for x in -1..=1 {
212+
for y in -1..=1 {
213+
let x = x + i as i32;
214+
let y = y + j as i32;
215+
if x < 0 || x == n as i32 || y < 0 || y == n as i32 {
216+
continue;
217+
}
218+
queue.push_back([x as usize, y as usize]);
219+
}
220+
}
221+
}
222+
}
223+
-1
224+
}
225+
}
226+
```
227+
190228
### **...**
191229

192230
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
use std::collections::VecDeque;
2+
impl Solution {
3+
pub fn shortest_path_binary_matrix(mut grid: Vec<Vec<i32>>) -> i32 {
4+
let n = grid.len();
5+
let mut queue = VecDeque::new();
6+
queue.push_back([0, 0]);
7+
let mut res = 0;
8+
while !queue.is_empty() {
9+
res += 1;
10+
for _ in 0..queue.len() {
11+
let [i, j] = queue.pop_front().unwrap();
12+
if grid[i][j] == 1 {
13+
continue;
14+
}
15+
if i == n - 1 && j == n - 1 {
16+
return res;
17+
}
18+
grid[i][j] = 1;
19+
for x in -1..=1 {
20+
for y in -1..=1 {
21+
let x = x + i as i32;
22+
let y = y + j as i32;
23+
if x < 0 || x == n as i32 || y < 0 || y == n as i32 {
24+
continue;
25+
}
26+
queue.push_back([x as usize, y as usize]);
27+
}
28+
}
29+
}
30+
}
31+
-1
32+
}
33+
}

0 commit comments

Comments
 (0)