From b19b832edf8cbe20ce3a9736e292c06b9d4fe979 Mon Sep 17 00:00:00 2001 From: fwqaaq Date: Tue, 16 Jan 2024 14:00:17 +0800 Subject: [PATCH 1/2] =?UTF-8?q?Update=200417.=E5=A4=AA=E5=B9=B3=E6=B4=8B?= =?UTF-8?q?=E5=A4=A7=E8=A5=BF=E6=B4=8B=E6=B0=B4=E6=B5=81=E9=97=AE=E9=A2=98?= =?UTF-8?q?.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../0417.太平洋大西洋水流问题.md | 102 ++++++++++++++++++ 1 file changed, 102 insertions(+) diff --git a/problems/0417.太平洋大西洋水流问题.md b/problems/0417.太平洋大西洋水流问题.md index 53ae14ed..21f8a1ca 100644 --- a/problems/0417.太平洋大西洋水流问题.md +++ b/problems/0417.太平洋大西洋水流问题.md @@ -562,7 +562,109 @@ function pacificAtlantic(heights) { } ``` +### go +dfs: + +```go +var DIRECTIONS = [4][2]int{{-1, 0}, {1, 0}, {0, -1}, {0, 1}} + +func pacificAtlantic(heights [][]int) [][]int { + res := make([][]int, 0) + pacific := make([][]bool, len(heights)) + atlantic := make([][]bool, len(heights)) + for i := 0; i < len(heights); i++ { + pacific[i] = make([]bool, len(heights[0])) + atlantic[i] = make([]bool, len(heights[0])) + } + // 列 + for i := 0; i < len(heights); i++ { + dfs(heights, pacific, i, 0) + dfs(heights, atlantic, i, len(heights[0])-1) + } + // 行 + for j := 0; j < len(heights[0]); j++ { + dfs(heights, pacific, 0, j) + dfs(heights, atlantic, len(heights)-1, j) + } + + for i := 0; i < len(heights); i++ { + for j := 0; j < len(heights[0]); j++ { + if pacific[i][j] && atlantic[i][j] { + res = append(res, []int{i, j}) + } + } + } + + return res +} + +func dfs(heights [][]int, visited [][]bool, i, j int) { + visited[i][j] = true + for _, d := range DIRECTIONS { + x, y := i+d[0], j+d[1] + if x < 0 || x >= len(heights) || y < 0 || y >= len(heights[0]) || heights[i][j] > heights[x][y] || visited[x][y] { + continue + } + + dfs(heights, visited, x, y) + } +} +``` + +bfs: + +```go +var DIRECTIONS = [4][2]int{{-1, 0}, {1, 0}, {0, -1}, {0, 1}} + +func pacificAtlantic(heights [][]int) [][]int { + res := make([][]int, 0) + pacific := make([][]bool, len(heights)) + atlantic := make([][]bool, len(heights)) + for i := 0; i < len(heights); i++ { + pacific[i] = make([]bool, len(heights[0])) + atlantic[i] = make([]bool, len(heights[0])) + } + // 列 + for i := 0; i < len(heights); i++ { + bfs(heights, pacific, i, 0) + bfs(heights, atlantic, i, len(heights[0])-1) + } + // 行 + for j := 0; j < len(heights[0]); j++ { + bfs(heights, pacific, 0, j) + bfs(heights, atlantic, len(heights)-1, j) + } + + for i := 0; i < len(heights); i++ { + for j := 0; j < len(heights[0]); j++ { + if pacific[i][j] && atlantic[i][j] { + res = append(res, []int{i, j}) + } + } + } + + return res +} + +func bfs(heights [][]int, visited [][]bool, i, j int) { + queue := make([][]int, 0) + queue = append(queue, []int{i, j}) + visited[i][j] = true + for len(queue) > 0 { + cur := queue[0] + queue = queue[1:] + for _, d := range DIRECTIONS { + x, y := cur[0]+d[0], cur[1]+d[1] + if x < 0 || x >= len(heights) || y < 0 || y >= len(heights[0]) || heights[cur[0]][cur[1]] > heights[x][y] || visited[x][y] { + continue + } + queue = append(queue, []int{x, y}) + visited[x][y] = true + } + } +} +```

From f9c2d9677bdf6432c87b4c91b4596fc634599c93 Mon Sep 17 00:00:00 2001 From: fwqaaq Date: Tue, 16 Jan 2024 14:35:10 +0800 Subject: [PATCH 2/2] =?UTF-8?q?Update=200417.=E5=A4=AA=E5=B9=B3=E6=B4=8B?= =?UTF-8?q?=E5=A4=A7=E8=A5=BF=E6=B4=8B=E6=B0=B4=E6=B5=81=E9=97=AE=E9=A2=98?= =?UTF-8?q?.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../0417.太平洋大西洋水流问题.md | 104 ++++++++++++++++++ 1 file changed, 104 insertions(+) diff --git a/problems/0417.太平洋大西洋水流问题.md b/problems/0417.太平洋大西洋水流问题.md index 21f8a1ca..8fe0f1b4 100644 --- a/problems/0417.太平洋大西洋水流问题.md +++ b/problems/0417.太平洋大西洋水流问题.md @@ -666,6 +666,110 @@ func bfs(heights [][]int, visited [][]bool, i, j int) { } ``` +### Rust + +dfs: + +```rust +impl Solution { + const DIRECTIONS: [(isize, isize); 4] = [(0, 1), (0, -1), (1, 0), (-1, 0)]; + pub fn pacific_atlantic(heights: Vec>) -> Vec> { + let (m, n) = (heights.len(), heights[0].len()); + let mut res = vec![]; + let (mut pacific, mut atlantic) = (vec![vec![false; n]; m], vec![vec![false; n]; m]); + + // 列 + for i in 0..m { + Self::dfs(&heights, &mut pacific, i, 0); + Self::dfs(&heights, &mut atlantic, i, n - 1); + } + + for j in 0..n { + Self::dfs(&heights, &mut pacific, 0, j); + Self::dfs(&heights, &mut atlantic, m - 1, j); + } + + for i in 0..m { + for j in 0..n { + if pacific[i][j] && atlantic[i][j] { + res.push(vec![i as i32, j as i32]); + } + } + } + + res + } + + pub fn dfs(heights: &[Vec], visited: &mut [Vec], i: usize, j: usize) { + visited[i][j] = true; + for (dx, dy) in Self::DIRECTIONS { + let (x, y) = (i as isize + dx, j as isize + dy); + if x < 0 || x >= heights.len() as isize || y < 0 || y >= heights[0].len() as isize { + continue; + } + let (x, y) = (x as usize, y as usize); + if !visited[x][y] && heights[x][y] >= heights[i][j] { + Self::dfs(heights, visited, x, y); + } + } + } +} +``` + +bfs: + +```rust +use std::collections::VecDeque; + +impl Solution { + const DIRECTIONS: [(isize, isize); 4] = [(0, 1), (0, -1), (1, 0), (-1, 0)]; + pub fn pacific_atlantic(heights: Vec>) -> Vec> { + let (m, n) = (heights.len(), heights[0].len()); + let mut res = vec![]; + let (mut pacific, mut atlantic) = (vec![vec![false; n]; m], vec![vec![false; n]; m]); + + // 列 + for i in 0..m { + Self::bfs(&heights, &mut pacific, i, 0); + Self::bfs(&heights, &mut atlantic, i, n - 1); + } + + for j in 0..n { + Self::bfs(&heights, &mut pacific, 0, j); + Self::bfs(&heights, &mut atlantic, m - 1, j); + } + + for i in 0..m { + for j in 0..n { + if pacific[i][j] && atlantic[i][j] { + res.push(vec![i as i32, j as i32]); + } + } + } + + res + } + + pub fn bfs(heights: &[Vec], visited: &mut [Vec], i: usize, j: usize) { + let mut queue = VecDeque::from([(i, j)]); + visited[i][j] = true; + while let Some((i, j)) = queue.pop_front() { + for (dx, dy) in Self::DIRECTIONS { + let (x, y) = (i as isize + dx, j as isize + dy); + if x < 0 || x >= heights.len() as isize || y < 0 || y >= heights[0].len() as isize { + continue; + } + let (x, y) = (x as usize, y as usize); + if !visited[x][y] && heights[x][y] >= heights[i][j] { + queue.push_back((x, y)); + visited[x][y] = true; + } + } + } + } +} +``` +