Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 51 additions & 0 deletions solution/0200-0299/0289.Game of Life/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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<Vec<i32>>) {
let n = board.len();
let m = board[0].len();
let mut weight_vec: Vec<Vec<i32>> = 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
Expand Down
51 changes: 51 additions & 0 deletions solution/0200-0299/0289.Game of Life/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -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<Vec<i32>>) {
let n = board.len();
let m = board[0].len();
let mut weight_vec: Vec<Vec<i32>> = 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
Expand Down
46 changes: 46 additions & 0 deletions solution/0200-0299/0289.Game of Life/Solution.rs
Original file line number Diff line number Diff line change
@@ -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<Vec<i32>>) {
let n = board.len();
let m = board[0].len();
let mut weight_vec: Vec<Vec<i32>> = 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
}
}