Skip to content

Commit 1c3d9d7

Browse files
authored
feat: add rust solution to lc problem: No.0289 (#1253)
1 parent 7f69bc6 commit 1c3d9d7

File tree

3 files changed

+148
-0
lines changed

3 files changed

+148
-0
lines changed

solution/0200-0299/0289.Game of Life/README.md

+51
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,57 @@ public:
175175
};
176176
```
177177
178+
### **Rust**
179+
180+
```rust
181+
const DIR: [(i32, i32); 8] = [(-1, 0), (1, 0), (0, -1), (0, 1), (-1, -1), (-1, 1), (1, -1), (1, 1)];
182+
183+
impl Solution {
184+
#[allow(dead_code)]
185+
pub fn game_of_life(board: &mut Vec<Vec<i32>>) {
186+
let n = board.len();
187+
let m = board[0].len();
188+
let mut weight_vec: Vec<Vec<i32>> = vec![vec![0; m]; n];
189+
190+
// Initialize the weight vector
191+
for i in 0..n {
192+
for j in 0..m {
193+
if board[i][j] == 0 {
194+
continue;
195+
}
196+
for (dx, dy) in DIR {
197+
let x = i as i32 + dx;
198+
let y = j as i32 + dy;
199+
if Self::check_bounds(x, y, n as i32, m as i32) {
200+
weight_vec[x as usize][y as usize] += 1;
201+
}
202+
}
203+
}
204+
}
205+
206+
// Update the board
207+
for i in 0..n {
208+
for j in 0..m {
209+
if weight_vec[i][j] < 2 {
210+
board[i][j] = 0;
211+
} else if weight_vec[i][j] <= 3 {
212+
if board[i][j] == 0 && weight_vec[i][j] == 3 {
213+
board[i][j] = 1;
214+
}
215+
} else {
216+
board[i][j] = 0;
217+
}
218+
}
219+
}
220+
}
221+
222+
#[allow(dead_code)]
223+
fn check_bounds(i: i32, j: i32, n: i32, m: i32) -> bool {
224+
i >= 0 && i < n && j >= 0 && j < m
225+
}
226+
}
227+
```
228+
178229
### **Go**
179230

180231
```go

solution/0200-0299/0289.Game of Life/README_EN.md

+51
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,57 @@ public:
164164
};
165165
```
166166
167+
### **Rust**
168+
169+
```rust
170+
const DIR: [(i32, i32); 8] = [(-1, 0), (1, 0), (0, -1), (0, 1), (-1, -1), (-1, 1), (1, -1), (1, 1)];
171+
172+
impl Solution {
173+
#[allow(dead_code)]
174+
pub fn game_of_life(board: &mut Vec<Vec<i32>>) {
175+
let n = board.len();
176+
let m = board[0].len();
177+
let mut weight_vec: Vec<Vec<i32>> = vec![vec![0; m]; n];
178+
179+
// Initialize the weight vector
180+
for i in 0..n {
181+
for j in 0..m {
182+
if board[i][j] == 0 {
183+
continue;
184+
}
185+
for (dx, dy) in DIR {
186+
let x = i as i32 + dx;
187+
let y = j as i32 + dy;
188+
if Self::check_bounds(x, y, n as i32, m as i32) {
189+
weight_vec[x as usize][y as usize] += 1;
190+
}
191+
}
192+
}
193+
}
194+
195+
// Update the board
196+
for i in 0..n {
197+
for j in 0..m {
198+
if weight_vec[i][j] < 2 {
199+
board[i][j] = 0;
200+
} else if weight_vec[i][j] <= 3 {
201+
if board[i][j] == 0 && weight_vec[i][j] == 3 {
202+
board[i][j] = 1;
203+
}
204+
} else {
205+
board[i][j] = 0;
206+
}
207+
}
208+
}
209+
}
210+
211+
#[allow(dead_code)]
212+
fn check_bounds(i: i32, j: i32, n: i32, m: i32) -> bool {
213+
i >= 0 && i < n && j >= 0 && j < m
214+
}
215+
}
216+
```
217+
167218
### **Go**
168219

169220
```go
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
const DIR: [(i32, i32); 8] = [(-1, 0), (1, 0), (0, -1), (0, 1), (-1, -1), (-1, 1), (1, -1), (1, 1)];
2+
3+
impl Solution {
4+
#[allow(dead_code)]
5+
pub fn game_of_life(board: &mut Vec<Vec<i32>>) {
6+
let n = board.len();
7+
let m = board[0].len();
8+
let mut weight_vec: Vec<Vec<i32>> = vec![vec![0; m]; n];
9+
10+
// Initialize the weight vector
11+
for i in 0..n {
12+
for j in 0..m {
13+
if board[i][j] == 0 {
14+
continue;
15+
}
16+
for (dx, dy) in DIR {
17+
let x = i as i32 + dx;
18+
let y = j as i32 + dy;
19+
if Self::check_bounds(x, y, n as i32, m as i32) {
20+
weight_vec[x as usize][y as usize] += 1;
21+
}
22+
}
23+
}
24+
}
25+
26+
// Update the board
27+
for i in 0..n {
28+
for j in 0..m {
29+
if weight_vec[i][j] < 2 {
30+
board[i][j] = 0;
31+
} else if weight_vec[i][j] <= 3 {
32+
if board[i][j] == 0 && weight_vec[i][j] == 3 {
33+
board[i][j] = 1;
34+
}
35+
} else {
36+
board[i][j] = 0;
37+
}
38+
}
39+
}
40+
}
41+
42+
#[allow(dead_code)]
43+
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)