Skip to content

Commit 27610b1

Browse files
committed
feat: add rust solution to lcci problems: No.08.09,08.10
- No.08.09.Bracket - No.08.10.Color Fill
1 parent 7888203 commit 27610b1

File tree

6 files changed

+170
-0
lines changed

6 files changed

+170
-0
lines changed

lcci/08.09.Bracket/README.md

+25
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,31 @@ function dfs(n, left, right, prev, res) {
172172
}
173173
```
174174

175+
### **Rust**
176+
177+
```rust
178+
impl Solution {
179+
fn dfs(left: i32, right: i32, t: String, res: &mut Vec<String>) {
180+
if left == 0 && right == 0 {
181+
res.push(t);
182+
return;
183+
}
184+
if left > 0 {
185+
Self::dfs(left - 1, right, format!("{}(", t), res);
186+
}
187+
if left < right {
188+
Self::dfs(left, right - 1, format!("{})", t), res);
189+
}
190+
}
191+
192+
pub fn generate_parenthesis(n: i32) -> Vec<String> {
193+
let mut res = vec![];
194+
Self::dfs(n, n, String::new(), &mut res);
195+
res
196+
}
197+
}
198+
```
199+
175200
### **...**
176201

177202
```

lcci/08.09.Bracket/README_EN.md

+25
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,31 @@ function dfs(n, left, right, prev, res) {
173173
}
174174
```
175175

176+
### **Rust**
177+
178+
```rust
179+
impl Solution {
180+
fn dfs(left: i32, right: i32, t: String, res: &mut Vec<String>) {
181+
if left == 0 && right == 0 {
182+
res.push(t);
183+
return;
184+
}
185+
if left > 0 {
186+
Self::dfs(left - 1, right, format!("{}(", t), res);
187+
}
188+
if left < right {
189+
Self::dfs(left, right - 1, format!("{})", t), res);
190+
}
191+
}
192+
193+
pub fn generate_parenthesis(n: i32) -> Vec<String> {
194+
let mut res = vec![];
195+
Self::dfs(n, n, String::new(), &mut res);
196+
res
197+
}
198+
}
199+
```
200+
176201
### **...**
177202

178203
```

lcci/08.09.Bracket/Solution.rs

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
impl Solution {
2+
fn dfs(left: i32, right: i32, t: String, res: &mut Vec<String>) {
3+
if left == 0 && right == 0 {
4+
res.push(t);
5+
return;
6+
}
7+
if left > 0 {
8+
Self::dfs(left - 1, right, format!("{}(", t), res);
9+
}
10+
if left < right {
11+
Self::dfs(left, right - 1, format!("{})", t), res);
12+
}
13+
}
14+
15+
pub fn generate_parenthesis(n: i32) -> Vec<String> {
16+
let mut res = vec![];
17+
Self::dfs(n, n, String::new(), &mut res);
18+
res
19+
}
20+
}

lcci/08.10.Color Fill/README.md

+35
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,41 @@ func dfs(image [][]int, i, j, oc, nc int) {
120120
}
121121
```
122122

123+
### **Rust**
124+
125+
```rust
126+
impl Solution {
127+
fn dfs(i: usize, j: usize, target: i32, new_color: i32, image: &mut Vec<Vec<i32>>) {
128+
if image[i][j] != target {
129+
return;
130+
}
131+
image[i][j] = new_color;
132+
if i != 0 {
133+
Self::dfs(i - 1, j, target, new_color, image);
134+
}
135+
if j != 0 {
136+
Self::dfs(i, j - 1, target, new_color, image);
137+
}
138+
if i + 1 != image.len() {
139+
Self::dfs(i + 1, j, target, new_color, image);
140+
}
141+
if j + 1 != image[0].len() {
142+
Self::dfs(i, j + 1, target, new_color, image);
143+
}
144+
}
145+
146+
pub fn flood_fill(mut image: Vec<Vec<i32>>, sr: i32, sc: i32, new_color: i32) -> Vec<Vec<i32>> {
147+
let (sr, sc) = (sr as usize, sc as usize);
148+
let target = image[sr][sc];
149+
if target == new_color {
150+
return image;
151+
}
152+
Self::dfs(sr, sc, target, new_color, &mut image);
153+
image
154+
}
155+
}
156+
```
157+
123158
### **...**
124159

125160
```

lcci/08.10.Color Fill/README_EN.md

+35
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,41 @@ func dfs(image [][]int, i, j, oc, nc int) {
121121
}
122122
```
123123

124+
### **Rust**
125+
126+
```rust
127+
impl Solution {
128+
fn dfs(i: usize, j: usize, target: i32, new_color: i32, image: &mut Vec<Vec<i32>>) {
129+
if image[i][j] != target {
130+
return;
131+
}
132+
image[i][j] = new_color;
133+
if i != 0 {
134+
Self::dfs(i - 1, j, target, new_color, image);
135+
}
136+
if j != 0 {
137+
Self::dfs(i, j - 1, target, new_color, image);
138+
}
139+
if i + 1 != image.len() {
140+
Self::dfs(i + 1, j, target, new_color, image);
141+
}
142+
if j + 1 != image[0].len() {
143+
Self::dfs(i, j + 1, target, new_color, image);
144+
}
145+
}
146+
147+
pub fn flood_fill(mut image: Vec<Vec<i32>>, sr: i32, sc: i32, new_color: i32) -> Vec<Vec<i32>> {
148+
let (sr, sc) = (sr as usize, sc as usize);
149+
let target = image[sr][sc];
150+
if target == new_color {
151+
return image;
152+
}
153+
Self::dfs(sr, sc, target, new_color, &mut image);
154+
image
155+
}
156+
}
157+
```
158+
124159
### **...**
125160

126161
```

lcci/08.10.Color Fill/Solution.rs

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
impl Solution {
2+
fn dfs(i: usize, j: usize, target: i32, new_color: i32, image: &mut Vec<Vec<i32>>) {
3+
if image[i][j] != target {
4+
return;
5+
}
6+
image[i][j] = new_color;
7+
if i != 0 {
8+
Self::dfs(i - 1, j, target, new_color, image);
9+
}
10+
if j != 0 {
11+
Self::dfs(i, j - 1, target, new_color, image);
12+
}
13+
if i + 1 != image.len() {
14+
Self::dfs(i + 1, j, target, new_color, image);
15+
}
16+
if j + 1 != image[0].len() {
17+
Self::dfs(i, j + 1, target, new_color, image);
18+
}
19+
}
20+
21+
pub fn flood_fill(mut image: Vec<Vec<i32>>, sr: i32, sc: i32, new_color: i32) -> Vec<Vec<i32>> {
22+
let (sr, sc) = (sr as usize, sc as usize);
23+
let target = image[sr][sc];
24+
if target == new_color {
25+
return image;
26+
}
27+
Self::dfs(sr, sc, target, new_color, &mut image);
28+
image
29+
}
30+
}

0 commit comments

Comments
 (0)