From eedb042024fe26857c038a8bfc7490c9829462e5 Mon Sep 17 00:00:00 2001 From: fwqaaq Date: Mon, 21 Aug 2023 22:00:22 +0800 Subject: [PATCH 1/4] =?UTF-8?q?Update=200695.=E5=B2=9B=E5=B1=BF=E7=9A=84?= =?UTF-8?q?=E6=9C=80=E5=A4=A7=E9=9D=A2=E7=A7=AF.md=20about=20rust?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0695.岛屿的最大面积.md | 49 ++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) diff --git a/problems/0695.岛屿的最大面积.md b/problems/0695.岛屿的最大面积.md index 186f044c..9ce4e56c 100644 --- a/problems/0695.岛屿的最大面积.md +++ b/problems/0695.岛屿的最大面积.md @@ -390,6 +390,55 @@ class Solution: if 0 <= new_x < len(grid) and 0 <= new_y < len(grid[0]): self.dfs(grid, visited, new_x, new_y) ``` + +### Rust + +dfs: 版本一 + +```rust +impl Solution { + const DIRECTIONS: [(i32, i32); 4] = [(0, 1), (1, 0), (-1, 0), (0, -1)]; + + pub fn max_area_of_island(grid: Vec>) -> i32 { + let mut visited = vec![vec![false; grid[0].len()]; grid.len()]; + + let mut res = 0; + for (i, nums) in grid.iter().enumerate() { + for (j, &num) in nums.iter().enumerate() { + if !visited[i][j] && num == 1 { + let mut count = 1; + visited[i][j] = true; + Self::dfs(&grid, &mut visited, (i as i32, j as i32), &mut count); + res = res.max(count); + } + } + } + + res + } + + pub fn dfs( + grid: &Vec>, + visited: &mut [Vec], + (x, y): (i32, i32), + count: &mut i32, + ) { + for (dx, dy) in Self::DIRECTIONS { + let (nx, ny) = (x + dx, y + dy); + if nx < 0 || nx >= grid.len() as i32 || ny < 0 || ny >= grid[0].len() as i32 { + continue; + } + let (nx, ny) = (nx as usize, ny as usize); + if !visited[nx][ny] && grid[nx][ny] == 1 { + visited[nx][ny] = true; + *count += 1; + Self::dfs(grid, visited, (nx as i32, ny as i32), count); + } + } + } +} +``` +

From d02c6a7d178ed420dfa404e091b6f7da0118185f Mon Sep 17 00:00:00 2001 From: fwqaaq Date: Mon, 21 Aug 2023 22:09:24 +0800 Subject: [PATCH 2/4] =?UTF-8?q?Update=200695.=E5=B2=9B=E5=B1=BF=E7=9A=84?= =?UTF-8?q?=E6=9C=80=E5=A4=A7=E9=9D=A2=E7=A7=AF.md=20about=20rust?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0695.岛屿的最大面积.md | 45 ++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) diff --git a/problems/0695.岛屿的最大面积.md b/problems/0695.岛屿的最大面积.md index 9ce4e56c..2d25c54c 100644 --- a/problems/0695.岛屿的最大面积.md +++ b/problems/0695.岛屿的最大面积.md @@ -439,6 +439,51 @@ impl Solution { } ``` +dfs: 版本二 + +```rust +impl Solution { + const DIRECTIONS: [(i32, i32); 4] = [(0, 1), (1, 0), (-1, 0), (0, -1)]; + + pub fn max_area_of_island(grid: Vec>) -> i32 { + let mut visited = vec![vec![false; grid[0].len()]; grid.len()]; + + let mut res = 0; + for (i, nums) in grid.iter().enumerate() { + for (j, &num) in nums.iter().enumerate() { + if !visited[i][j] && num == 1 { + let mut count = 0; + Self::dfs(&grid, &mut visited, (i as i32, j as i32), &mut count); + res = res.max(count); + } + } + } + + res + } + + pub fn dfs( + grid: &Vec>, + visited: &mut [Vec], + (x, y): (i32, i32), + count: &mut i32, + ) { + if visited[x as usize][y as usize] || grid[x as usize][y as usize] == 0 { + return; + } + visited[x as usize][y as usize] = true; + *count += 1; + for (dx, dy) in Self::DIRECTIONS { + let (nx, ny) = (x + dx, y + dy); + if nx < 0 || nx >= grid.len() as i32 || ny < 0 || ny >= grid[0].len() as i32 { + continue; + } + Self::dfs(grid, visited, (nx, ny), count); + } + } +} +``` +

From c7ec1d7705b108257983f1b192126f2bec2888b4 Mon Sep 17 00:00:00 2001 From: fwqaaq Date: Mon, 21 Aug 2023 22:27:28 +0800 Subject: [PATCH 3/4] =?UTF-8?q?Update=200695.=E5=B2=9B=E5=B1=BF=E7=9A=84?= =?UTF-8?q?=E6=9C=80=E5=A4=A7=E9=9D=A2=E7=A7=AF.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0695.岛屿的最大面积.md | 47 ++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) diff --git a/problems/0695.岛屿的最大面积.md b/problems/0695.岛屿的最大面积.md index 2d25c54c..b8b2ea72 100644 --- a/problems/0695.岛屿的最大面积.md +++ b/problems/0695.岛屿的最大面积.md @@ -484,6 +484,53 @@ impl Solution { } ``` +bfs: + +```rust +use std::collections::VecDeque; +impl Solution { + const DIRECTIONS: [(i32, i32); 4] = [(0, 1), (1, 0), (-1, 0), (0, -1)]; + + pub fn max_area_of_island(grid: Vec>) -> i32 { + let mut visited = vec![vec![false; grid[0].len()]; grid.len()]; + + let mut res = 0; + for (i, nums) in grid.iter().enumerate() { + for (j, &num) in nums.iter().enumerate() { + if !visited[i][j] && num == 1 { + let mut count = 0; + Self::bfs(&grid, &mut visited, (i as i32, j as i32), &mut count); + res = res.max(count); + } + } + } + + res + } + + pub fn bfs(grid: &[Vec], visited: &mut [Vec], (x, y): (i32, i32), count: &mut i32) { + let mut queue = VecDeque::new(); + queue.push_back((x, y)); + visited[x as usize][y as usize] = true; + *count += 1; + while let Some((cur_x, cur_y)) = queue.pop_front() { + for (dx, dy) in Self::DIRECTIONS { + let (nx, ny) = (cur_x + dx, cur_y + dy); + if nx < 0 || nx >= grid.len() as i32 || ny < 0 || ny >= grid[0].len() as i32 { + continue; + } + let (nx, ny) = (nx as usize, ny as usize); + if !visited[nx][ny] && grid[nx][ny] == 1 { + visited[nx][ny] = true; + queue.push_back((nx as i32, ny as i32)); + *count += 1; + } + } + } + } +} +``` +

From 1a54008ef0456c41c9c20a2b0bb4438c284bee1f Mon Sep 17 00:00:00 2001 From: fwqaaq Date: Mon, 21 Aug 2023 22:32:55 +0800 Subject: [PATCH 4/4] Apply suggestions from code review --- problems/0695.岛屿的最大面积.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/problems/0695.岛屿的最大面积.md b/problems/0695.岛屿的最大面积.md index b8b2ea72..3dd181a3 100644 --- a/problems/0695.岛屿的最大面积.md +++ b/problems/0695.岛屿的最大面积.md @@ -418,7 +418,7 @@ impl Solution { } pub fn dfs( - grid: &Vec>, + grid: &[Vec], visited: &mut [Vec], (x, y): (i32, i32), count: &mut i32, @@ -463,7 +463,7 @@ impl Solution { } pub fn dfs( - grid: &Vec>, + grid: &[Vec], visited: &mut [Vec], (x, y): (i32, i32), count: &mut i32,