Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit f9c2d96

Browse files
authored
Update 0417.太平洋大西洋水流问题.md
1 parent b19b832 commit f9c2d96

File tree

1 file changed

+104
-0
lines changed

1 file changed

+104
-0
lines changed

‎problems/0417.太平洋大西洋水流问题.md‎

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -666,6 +666,110 @@ func bfs(heights [][]int, visited [][]bool, i, j int) {
666666
}
667667
```
668668

669+
### Rust
670+
671+
dfs:
672+
673+
```rust
674+
impl Solution {
675+
const DIRECTIONS: [(isize, isize); 4] = [(0, 1), (0, -1), (1, 0), (-1, 0)];
676+
pub fn pacific_atlantic(heights: Vec<Vec<i32>>) -> Vec<Vec<i32>> {
677+
let (m, n) = (heights.len(), heights[0].len());
678+
let mut res = vec![];
679+
let (mut pacific, mut atlantic) = (vec![vec![false; n]; m], vec![vec![false; n]; m]);
680+
681+
//
682+
for i in 0..m {
683+
Self::dfs(&heights, &mut pacific, i, 0);
684+
Self::dfs(&heights, &mut atlantic, i, n - 1);
685+
}
686+
687+
for j in 0..n {
688+
Self::dfs(&heights, &mut pacific, 0, j);
689+
Self::dfs(&heights, &mut atlantic, m - 1, j);
690+
}
691+
692+
for i in 0..m {
693+
for j in 0..n {
694+
if pacific[i][j] && atlantic[i][j] {
695+
res.push(vec![i as i32, j as i32]);
696+
}
697+
}
698+
}
699+
700+
res
701+
}
702+
703+
pub fn dfs(heights: &[Vec<i32>], visited: &mut [Vec<bool>], i: usize, j: usize) {
704+
visited[i][j] = true;
705+
for (dx, dy) in Self::DIRECTIONS {
706+
let (x, y) = (i as isize + dx, j as isize + dy);
707+
if x < 0 || x >= heights.len() as isize || y < 0 || y >= heights[0].len() as isize {
708+
continue;
709+
}
710+
let (x, y) = (x as usize, y as usize);
711+
if !visited[x][y] && heights[x][y] >= heights[i][j] {
712+
Self::dfs(heights, visited, x, y);
713+
}
714+
}
715+
}
716+
}
717+
```
718+
719+
bfs:
720+
721+
```rust
722+
use std::collections::VecDeque;
723+
724+
impl Solution {
725+
const DIRECTIONS: [(isize, isize); 4] = [(0, 1), (0, -1), (1, 0), (-1, 0)];
726+
pub fn pacific_atlantic(heights: Vec<Vec<i32>>) -> Vec<Vec<i32>> {
727+
let (m, n) = (heights.len(), heights[0].len());
728+
let mut res = vec![];
729+
let (mut pacific, mut atlantic) = (vec![vec![false; n]; m], vec![vec![false; n]; m]);
730+
731+
//
732+
for i in 0..m {
733+
Self::bfs(&heights, &mut pacific, i, 0);
734+
Self::bfs(&heights, &mut atlantic, i, n - 1);
735+
}
736+
737+
for j in 0..n {
738+
Self::bfs(&heights, &mut pacific, 0, j);
739+
Self::bfs(&heights, &mut atlantic, m - 1, j);
740+
}
741+
742+
for i in 0..m {
743+
for j in 0..n {
744+
if pacific[i][j] && atlantic[i][j] {
745+
res.push(vec![i as i32, j as i32]);
746+
}
747+
}
748+
}
749+
750+
res
751+
}
752+
753+
pub fn bfs(heights: &[Vec<i32>], visited: &mut [Vec<bool>], i: usize, j: usize) {
754+
let mut queue = VecDeque::from([(i, j)]);
755+
visited[i][j] = true;
756+
while let Some((i, j)) = queue.pop_front() {
757+
for (dx, dy) in Self::DIRECTIONS {
758+
let (x, y) = (i as isize + dx, j as isize + dy);
759+
if x < 0 || x >= heights.len() as isize || y < 0 || y >= heights[0].len() as isize {
760+
continue;
761+
}
762+
let (x, y) = (x as usize, y as usize);
763+
if !visited[x][y] && heights[x][y] >= heights[i][j] {
764+
queue.push_back((x, y));
765+
visited[x][y] = true;
766+
}
767+
}
768+
}
769+
}
770+
}
771+
```
772+
669773
<p align="center">
670774
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
671775
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /