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 9f8087b

Browse files
feat: add rust solutions to lc problems
* No.0200.Number of Islands * No.0460.LFU Cache
1 parent dcfb5c2 commit 9f8087b

File tree

9 files changed

+868
-3
lines changed

9 files changed

+868
-3
lines changed

‎solution/0200-0299/0200.Number of Islands/README.md

Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -610,6 +610,138 @@ func numIslands(grid [][]byte) int {
610610
}
611611
```
612612

613+
### **Rust**
614+
615+
DFS - Flood Fill 算法:
616+
617+
```rust
618+
const DIRS: [i32; 5] = [-1, 0, 1, 0, -1];
619+
620+
impl Solution {
621+
pub fn num_islands(grid: Vec<Vec<char>>) -> i32 {
622+
fn dfs(grid: &mut Vec<Vec<char>>, i: usize, j: usize) {
623+
grid[i][j] = '0';
624+
for k in 0..4 {
625+
let x = i as i32 + DIRS[k];
626+
let y = j as i32 + DIRS[k + 1];
627+
if x >= 0
628+
&& (x as usize) < grid.len()
629+
&& y >= 0
630+
&& (y as usize) < grid[0].len()
631+
&& grid[x as usize][y as usize] == '1'
632+
{
633+
dfs(grid, x as usize, y as usize);
634+
}
635+
}
636+
}
637+
638+
let mut grid = grid;
639+
let mut ans = 0;
640+
for i in 0..grid.len() {
641+
for j in 0..grid[0].len() {
642+
if grid[i][j] == '1' {
643+
dfs(&mut grid, i, j);
644+
ans += 1;
645+
}
646+
}
647+
}
648+
ans
649+
}
650+
}
651+
```
652+
653+
BFS - Flood Fill 算法:
654+
655+
```rust
656+
use std::collections::VecDeque;
657+
658+
const DIRS: [i32; 5] = [-1, 0, 1, 0, -1];
659+
660+
impl Solution {
661+
pub fn num_islands(grid: Vec<Vec<char>>) -> i32 {
662+
fn bfs(grid: &mut Vec<Vec<char>>, i: usize, j: usize) {
663+
grid[i][j] = '0';
664+
let mut queue = VecDeque::from([(i, j)]);
665+
while !queue.is_empty() {
666+
let (i, j) = queue.pop_front().unwrap();
667+
for k in 0..4 {
668+
let x = i as i32 + DIRS[k];
669+
let y = j as i32 + DIRS[k + 1];
670+
if x >= 0
671+
&& (x as usize) < grid.len()
672+
&& y >= 0
673+
&& (y as usize) < grid[0].len()
674+
&& grid[x as usize][y as usize] == '1'
675+
{
676+
grid[x as usize][y as usize] = '0';
677+
queue.push_back((x as usize, y as usize));
678+
}
679+
}
680+
}
681+
}
682+
683+
let mut grid = grid;
684+
let mut ans = 0;
685+
for i in 0..grid.len() {
686+
for j in 0..grid[0].len() {
687+
if grid[i][j] == '1' {
688+
bfs(&mut grid, i, j);
689+
ans += 1;
690+
}
691+
}
692+
}
693+
ans
694+
}
695+
}
696+
```
697+
698+
并查集:
699+
700+
```rust
701+
const DIRS: [usize; 3] = [1, 0, 1];
702+
703+
impl Solution {
704+
pub fn num_islands(grid: Vec<Vec<char>>) -> i32 {
705+
let m = grid.len();
706+
let n = grid[0].len();
707+
let mut p: Vec<i32> = (0..(m * n) as i32).collect();
708+
709+
fn find(p: &mut Vec<i32>, x: usize) -> i32 {
710+
if p[x] != x as i32 {
711+
p[x] = find(p, p[x] as usize);
712+
}
713+
p[x]
714+
}
715+
716+
for i in 0..m {
717+
for j in 0..n {
718+
if grid[i][j] == '1' {
719+
for k in 0..2 {
720+
let x = i + DIRS[k];
721+
let y = j + DIRS[k + 1];
722+
if x < m && y < n && grid[x][y] == '1' {
723+
let f1 = find(&mut p, x * n + y);
724+
let f2 = find(&mut p, i * n + j);
725+
p[f1 as usize] = f2;
726+
}
727+
}
728+
}
729+
}
730+
}
731+
732+
let mut ans = 0;
733+
for i in 0..m {
734+
for j in 0..n {
735+
if grid[i][j] == '1' && p[i * n + j] == (i * n + j) as i32 {
736+
ans += 1;
737+
}
738+
}
739+
}
740+
ans
741+
}
742+
}
743+
```
744+
613745
### **...**
614746

615747
```

‎solution/0200-0299/0200.Number of Islands/README_EN.md

Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -596,6 +596,138 @@ func numIslands(grid [][]byte) int {
596596
}
597597
```
598598

599+
### **Rust**
600+
601+
DFS:
602+
603+
```rust
604+
const DIRS: [i32; 5] = [-1, 0, 1, 0, -1];
605+
606+
impl Solution {
607+
pub fn num_islands(grid: Vec<Vec<char>>) -> i32 {
608+
fn dfs(grid: &mut Vec<Vec<char>>, i: usize, j: usize) {
609+
grid[i][j] = '0';
610+
for k in 0..4 {
611+
let x = i as i32 + DIRS[k];
612+
let y = j as i32 + DIRS[k + 1];
613+
if x >= 0
614+
&& (x as usize) < grid.len()
615+
&& y >= 0
616+
&& (y as usize) < grid[0].len()
617+
&& grid[x as usize][y as usize] == '1'
618+
{
619+
dfs(grid, x as usize, y as usize);
620+
}
621+
}
622+
}
623+
624+
let mut grid = grid;
625+
let mut ans = 0;
626+
for i in 0..grid.len() {
627+
for j in 0..grid[0].len() {
628+
if grid[i][j] == '1' {
629+
dfs(&mut grid, i, j);
630+
ans += 1;
631+
}
632+
}
633+
}
634+
ans
635+
}
636+
}
637+
```
638+
639+
BFS:
640+
641+
```rust
642+
use std::collections::VecDeque;
643+
644+
const DIRS: [i32; 5] = [-1, 0, 1, 0, -1];
645+
646+
impl Solution {
647+
pub fn num_islands(grid: Vec<Vec<char>>) -> i32 {
648+
fn bfs(grid: &mut Vec<Vec<char>>, i: usize, j: usize) {
649+
grid[i][j] = '0';
650+
let mut queue = VecDeque::from([(i, j)]);
651+
while !queue.is_empty() {
652+
let (i, j) = queue.pop_front().unwrap();
653+
for k in 0..4 {
654+
let x = i as i32 + DIRS[k];
655+
let y = j as i32 + DIRS[k + 1];
656+
if x >= 0
657+
&& (x as usize) < grid.len()
658+
&& y >= 0
659+
&& (y as usize) < grid[0].len()
660+
&& grid[x as usize][y as usize] == '1'
661+
{
662+
grid[x as usize][y as usize] = '0';
663+
queue.push_back((x as usize, y as usize));
664+
}
665+
}
666+
}
667+
}
668+
669+
let mut grid = grid;
670+
let mut ans = 0;
671+
for i in 0..grid.len() {
672+
for j in 0..grid[0].len() {
673+
if grid[i][j] == '1' {
674+
bfs(&mut grid, i, j);
675+
ans += 1;
676+
}
677+
}
678+
}
679+
ans
680+
}
681+
}
682+
```
683+
684+
Union find:
685+
686+
```rust
687+
const DIRS: [usize; 3] = [1, 0, 1];
688+
689+
impl Solution {
690+
pub fn num_islands(grid: Vec<Vec<char>>) -> i32 {
691+
let m = grid.len();
692+
let n = grid[0].len();
693+
let mut p: Vec<i32> = (0..(m * n) as i32).collect();
694+
695+
fn find(p: &mut Vec<i32>, x: usize) -> i32 {
696+
if p[x] != x as i32 {
697+
p[x] = find(p, p[x] as usize);
698+
}
699+
p[x]
700+
}
701+
702+
for i in 0..m {
703+
for j in 0..n {
704+
if grid[i][j] == '1' {
705+
for k in 0..2 {
706+
let x = i + DIRS[k];
707+
let y = j + DIRS[k + 1];
708+
if x < m && y < n && grid[x][y] == '1' {
709+
let f1 = find(&mut p, x * n + y);
710+
let f2 = find(&mut p, i * n + j);
711+
p[f1 as usize] = f2;
712+
}
713+
}
714+
}
715+
}
716+
}
717+
718+
let mut ans = 0;
719+
for i in 0..m {
720+
for j in 0..n {
721+
if grid[i][j] == '1' && p[i * n + j] == (i * n + j) as i32 {
722+
ans += 1;
723+
}
724+
}
725+
}
726+
ans
727+
}
728+
}
729+
```
730+
599731
### **...**
600732

601733
```
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
const DIRS: [i32; 5] = [-1, 0, 1, 0, -1];
2+
3+
impl Solution {
4+
pub fn num_islands(grid: Vec<Vec<char>>) -> i32 {
5+
fn dfs(grid: &mut Vec<Vec<char>>, i: usize, j: usize) {
6+
grid[i][j] = '0';
7+
for k in 0..4 {
8+
let x = i as i32 + DIRS[k];
9+
let y = j as i32 + DIRS[k + 1];
10+
if x >= 0
11+
&& (x as usize) < grid.len()
12+
&& y >= 0
13+
&& (y as usize) < grid[0].len()
14+
&& grid[x as usize][y as usize] == '1'
15+
{
16+
dfs(grid, x as usize, y as usize);
17+
}
18+
}
19+
}
20+
21+
let mut grid = grid;
22+
let mut ans = 0;
23+
for i in 0..grid.len() {
24+
for j in 0..grid[0].len() {
25+
if grid[i][j] == '1' {
26+
dfs(&mut grid, i, j);
27+
ans += 1;
28+
}
29+
}
30+
}
31+
ans
32+
}
33+
}

0 commit comments

Comments
(0)

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