Skip to content

Commit f0402af

Browse files
authored
feat: add rust solution to lc problem: No.0542 (doocs#1138)
1 parent e42339a commit f0402af

File tree

3 files changed

+160
-0
lines changed

3 files changed

+160
-0
lines changed

solution/0500-0599/0542.01 Matrix/README.md

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,57 @@ public:
155155
};
156156
```
157157
158+
### **Rust**
159+
160+
```rust
161+
use std::collections::VecDeque;
162+
163+
impl Solution {
164+
#[allow(dead_code)]
165+
pub fn update_matrix(mat: Vec<Vec<i32>>) -> Vec<Vec<i32>> {
166+
let n: usize = mat.len();
167+
let m: usize = mat[0].len();
168+
let mut ret_vec: Vec<Vec<i32>> = vec![vec![-1; m]; n];
169+
// The inner tuple is of <X, Y, Current Count>
170+
let mut the_q: VecDeque<(usize, usize)> = VecDeque::new();
171+
let traverse_vec: Vec<(i32, i32)> = vec![(-1, 0), (1, 0), (0, 1), (0, -1)];
172+
173+
// Initialize the queue
174+
for i in 0..n {
175+
for j in 0..m {
176+
if mat[i][j] == 0 {
177+
// For the zero cell, enqueue at first
178+
the_q.push_back((i, j));
179+
// Set to 0 in return vector
180+
ret_vec[i][j] = 0;
181+
}
182+
}
183+
}
184+
185+
while !the_q.is_empty() {
186+
let (x, y) = the_q.front().unwrap().clone();
187+
the_q.pop_front();
188+
for pair in &traverse_vec {
189+
let cur_x = pair.0 + x as i32;
190+
let cur_y = pair.1 + y as i32;
191+
if Solution::check_bounds(cur_x, cur_y, n as i32, m as i32) && ret_vec[cur_x as usize][cur_y as usize] == -1 {
192+
// The current cell has not be updated yet, and is also in bound
193+
ret_vec[cur_x as usize][cur_y as usize] = ret_vec[x][y] + 1;
194+
the_q.push_back((cur_x as usize, cur_y as usize));
195+
}
196+
}
197+
}
198+
199+
ret_vec
200+
}
201+
202+
#[allow(dead_code)]
203+
pub fn check_bounds(i: i32, j: i32, n: i32, m: i32) -> bool {
204+
i >= 0 && i < n && j >= 0 && j < m
205+
}
206+
}
207+
```
208+
158209
### **Go**
159210

160211
```go

solution/0500-0599/0542.01 Matrix/README_EN.md

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,18 @@
3737

3838
## Solutions
3939

40+
**Method One: Multi-source BFS**
41+
42+
Initialize the result matrix ans, where the distance of all zeros is 0, and thus the distance of all ones is -1.
43+
44+
Initialize a queue q to store the positions to be checked by BFS, and enqueue all positions of zeros.
45+
46+
Continually dequeue elements `p(i, j)` from queue q, inspecting the four neighboring points.
47+
48+
For each neighbor `(x, y)`, if `ans[x][y] = -1`, then update `ans[x][y] = ans[i][j] + 1`.
49+
50+
Also, enqueue the position `(x, y)`.
51+
4052
<!-- tabs:start -->
4153

4254
### **Python3**
@@ -135,6 +147,57 @@ public:
135147
};
136148
```
137149
150+
### **Rust**
151+
152+
```rust
153+
use std::collections::VecDeque;
154+
155+
impl Solution {
156+
#[allow(dead_code)]
157+
pub fn update_matrix(mat: Vec<Vec<i32>>) -> Vec<Vec<i32>> {
158+
let n: usize = mat.len();
159+
let m: usize = mat[0].len();
160+
let mut ret_vec: Vec<Vec<i32>> = vec![vec![-1; m]; n];
161+
// The inner tuple is of <X, Y, Current Count>
162+
let mut the_q: VecDeque<(usize, usize)> = VecDeque::new();
163+
let traverse_vec: Vec<(i32, i32)> = vec![(-1, 0), (1, 0), (0, 1), (0, -1)];
164+
165+
// Initialize the queue
166+
for i in 0..n {
167+
for j in 0..m {
168+
if mat[i][j] == 0 {
169+
// For the zero cell, enqueue at first
170+
the_q.push_back((i, j));
171+
// Set to 0 in return vector
172+
ret_vec[i][j] = 0;
173+
}
174+
}
175+
}
176+
177+
while !the_q.is_empty() {
178+
let (x, y) = the_q.front().unwrap().clone();
179+
the_q.pop_front();
180+
for pair in &traverse_vec {
181+
let cur_x = pair.0 + x as i32;
182+
let cur_y = pair.1 + y as i32;
183+
if Solution::check_bounds(cur_x, cur_y, n as i32, m as i32) && ret_vec[cur_x as usize][cur_y as usize] == -1 {
184+
// The current cell has not be updated yet, and is also in bound
185+
ret_vec[cur_x as usize][cur_y as usize] = ret_vec[x][y] + 1;
186+
the_q.push_back((cur_x as usize, cur_y as usize));
187+
}
188+
}
189+
}
190+
191+
ret_vec
192+
}
193+
194+
#[allow(dead_code)]
195+
pub fn check_bounds(i: i32, j: i32, n: i32, m: i32) -> bool {
196+
i >= 0 && i < n && j >= 0 && j < m
197+
}
198+
}
199+
```
200+
138201
### **Go**
139202

140203
```go
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
use std::collections::VecDeque;
2+
3+
impl Solution {
4+
#[allow(dead_code)]
5+
pub fn update_matrix(mat: Vec<Vec<i32>>) -> Vec<Vec<i32>> {
6+
let n: usize = mat.len();
7+
let m: usize = mat[0].len();
8+
let mut ret_vec: Vec<Vec<i32>> = vec![vec![-1; m]; n];
9+
// The inner tuple is of <X, Y, Current Count>
10+
let mut the_q: VecDeque<(usize, usize)> = VecDeque::new();
11+
let traverse_vec: Vec<(i32, i32)> = vec![(-1, 0), (1, 0), (0, 1), (0, -1)];
12+
13+
// Initialize the queue
14+
for i in 0..n {
15+
for j in 0..m {
16+
if mat[i][j] == 0 {
17+
// For the zero cell, enqueue at first
18+
the_q.push_back((i, j));
19+
// Set to 0 in return vector
20+
ret_vec[i][j] = 0;
21+
}
22+
}
23+
}
24+
25+
while !the_q.is_empty() {
26+
let (x, y) = the_q.front().unwrap().clone();
27+
the_q.pop_front();
28+
for pair in &traverse_vec {
29+
let cur_x = pair.0 + x as i32;
30+
let cur_y = pair.1 + y as i32;
31+
if Solution::check_bounds(cur_x, cur_y, n as i32, m as i32) && ret_vec[cur_x as usize][cur_y as usize] == -1 {
32+
// The current cell has not be updated yet, and is also in bound
33+
ret_vec[cur_x as usize][cur_y as usize] = ret_vec[x][y] + 1;
34+
the_q.push_back((cur_x as usize, cur_y as usize));
35+
}
36+
}
37+
}
38+
39+
ret_vec
40+
}
41+
42+
#[allow(dead_code)]
43+
pub fn check_bounds(i: i32, j: i32, n: i32, m: i32) -> bool {
44+
i >= 0 && i < n && j >= 0 && j < m
45+
}
46+
}

0 commit comments

Comments
 (0)