diff --git a/solution/0200-0299/0289.Game of Life/README.md b/solution/0200-0299/0289.Game of Life/README.md index a4549ebf07713..59923a2601b12 100644 --- a/solution/0200-0299/0289.Game of Life/README.md +++ b/solution/0200-0299/0289.Game of Life/README.md @@ -175,6 +175,57 @@ public: }; ``` +### **Rust** + +```rust +const DIR: [(i32, i32); 8] = [(-1, 0), (1, 0), (0, -1), (0, 1), (-1, -1), (-1, 1), (1, -1), (1, 1)]; + +impl Solution { + #[allow(dead_code)] + pub fn game_of_life(board: &mut Vec>) { + let n = board.len(); + let m = board[0].len(); + let mut weight_vec: Vec> = vec![vec![0; m]; n]; + + // Initialize the weight vector + for i in 0..n { + for j in 0..m { + if board[i][j] == 0 { + continue; + } + for (dx, dy) in DIR { + let x = i as i32 + dx; + let y = j as i32 + dy; + if Self::check_bounds(x, y, n as i32, m as i32) { + weight_vec[x as usize][y as usize] += 1; + } + } + } + } + + // Update the board + for i in 0..n { + for j in 0..m { + if weight_vec[i][j] < 2 { + board[i][j] = 0; + } else if weight_vec[i][j] <= 3 { + if board[i][j] == 0 && weight_vec[i][j] == 3 { + board[i][j] = 1; + } + } else { + board[i][j] = 0; + } + } + } + } + + #[allow(dead_code)] + fn check_bounds(i: i32, j: i32, n: i32, m: i32) -> bool { + i >= 0 && i < n && j >= 0 && j < m + } +} +``` + ### **Go** ```go diff --git a/solution/0200-0299/0289.Game of Life/README_EN.md b/solution/0200-0299/0289.Game of Life/README_EN.md index 2277185aec161..79633c3f54539 100644 --- a/solution/0200-0299/0289.Game of Life/README_EN.md +++ b/solution/0200-0299/0289.Game of Life/README_EN.md @@ -164,6 +164,57 @@ public: }; ``` +### **Rust** + +```rust +const DIR: [(i32, i32); 8] = [(-1, 0), (1, 0), (0, -1), (0, 1), (-1, -1), (-1, 1), (1, -1), (1, 1)]; + +impl Solution { + #[allow(dead_code)] + pub fn game_of_life(board: &mut Vec>) { + let n = board.len(); + let m = board[0].len(); + let mut weight_vec: Vec> = vec![vec![0; m]; n]; + + // Initialize the weight vector + for i in 0..n { + for j in 0..m { + if board[i][j] == 0 { + continue; + } + for (dx, dy) in DIR { + let x = i as i32 + dx; + let y = j as i32 + dy; + if Self::check_bounds(x, y, n as i32, m as i32) { + weight_vec[x as usize][y as usize] += 1; + } + } + } + } + + // Update the board + for i in 0..n { + for j in 0..m { + if weight_vec[i][j] < 2 { + board[i][j] = 0; + } else if weight_vec[i][j] <= 3 { + if board[i][j] == 0 && weight_vec[i][j] == 3 { + board[i][j] = 1; + } + } else { + board[i][j] = 0; + } + } + } + } + + #[allow(dead_code)] + fn check_bounds(i: i32, j: i32, n: i32, m: i32) -> bool { + i >= 0 && i < n && j >= 0 && j < m + } +} +``` + ### **Go** ```go diff --git a/solution/0200-0299/0289.Game of Life/Solution.rs b/solution/0200-0299/0289.Game of Life/Solution.rs new file mode 100644 index 0000000000000..990dad21b8f8e --- /dev/null +++ b/solution/0200-0299/0289.Game of Life/Solution.rs @@ -0,0 +1,46 @@ +const DIR: [(i32, i32); 8] = [(-1, 0), (1, 0), (0, -1), (0, 1), (-1, -1), (-1, 1), (1, -1), (1, 1)]; + +impl Solution { + #[allow(dead_code)] + pub fn game_of_life(board: &mut Vec>) { + let n = board.len(); + let m = board[0].len(); + let mut weight_vec: Vec> = vec![vec![0; m]; n]; + + // Initialize the weight vector + for i in 0..n { + for j in 0..m { + if board[i][j] == 0 { + continue; + } + for (dx, dy) in DIR { + let x = i as i32 + dx; + let y = j as i32 + dy; + if Self::check_bounds(x, y, n as i32, m as i32) { + weight_vec[x as usize][y as usize] += 1; + } + } + } + } + + // Update the board + for i in 0..n { + for j in 0..m { + if weight_vec[i][j] < 2 { + board[i][j] = 0; + } else if weight_vec[i][j] <= 3 { + if board[i][j] == 0 && weight_vec[i][j] == 3 { + board[i][j] = 1; + } + } else { + board[i][j] = 0; + } + } + } + } + + #[allow(dead_code)] + fn check_bounds(i: i32, j: i32, n: i32, m: i32) -> bool { + i >= 0 && i < n && j >= 0 && j < m + } +} \ No newline at end of file