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 b00d73a

Browse files
authored
feat: add rust solution to lc problem: No.0216 (doocs#1374)
1 parent ba683dd commit b00d73a

File tree

3 files changed

+94
-0
lines changed

3 files changed

+94
-0
lines changed

‎solution/0200-0299/0216.Combination Sum III/README.md‎

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -326,6 +326,39 @@ public:
326326
};
327327
```
328328
329+
### **Rust**
330+
331+
```rust
332+
impl Solution {
333+
#[allow(dead_code)]
334+
pub fn combination_sum3(k: i32, n: i32) -> Vec<Vec<i32>> {
335+
let mut ret = Vec::new();
336+
let mut candidates = (1..=9).collect();
337+
let mut cur_vec = Vec::new();
338+
Self::dfs(n, k, 0, 0, &mut cur_vec, &mut candidates, &mut ret);
339+
ret
340+
}
341+
342+
#[allow(dead_code)]
343+
fn dfs(target: i32, length: i32, cur_index: usize, cur_sum: i32, cur_vec: &mut Vec<i32>, candidates: &Vec<i32>, ans: &mut Vec<Vec<i32>>) {
344+
if cur_sum > target || cur_vec.len() > length as usize {
345+
// No answer for this
346+
return;
347+
}
348+
if cur_sum == target && cur_vec.len() == length as usize {
349+
// We get an answer
350+
ans.push(cur_vec.clone());
351+
return;
352+
}
353+
for i in cur_index..candidates.len() {
354+
cur_vec.push(candidates[i]);
355+
Self::dfs(target, length, i + 1, cur_sum + candidates[i], cur_vec, candidates, ans);
356+
cur_vec.pop().unwrap();
357+
}
358+
}
359+
}
360+
```
361+
329362
### **Go**
330363

331364
```go

‎solution/0200-0299/0216.Combination Sum III/README_EN.md‎

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -283,6 +283,39 @@ public:
283283
};
284284
```
285285
286+
### **Rust**
287+
288+
```rust
289+
impl Solution {
290+
#[allow(dead_code)]
291+
pub fn combination_sum3(k: i32, n: i32) -> Vec<Vec<i32>> {
292+
let mut ret = Vec::new();
293+
let mut candidates = (1..=9).collect();
294+
let mut cur_vec = Vec::new();
295+
Self::dfs(n, k, 0, 0, &mut cur_vec, &mut candidates, &mut ret);
296+
ret
297+
}
298+
299+
#[allow(dead_code)]
300+
fn dfs(target: i32, length: i32, cur_index: usize, cur_sum: i32, cur_vec: &mut Vec<i32>, candidates: &Vec<i32>, ans: &mut Vec<Vec<i32>>) {
301+
if cur_sum > target || cur_vec.len() > length as usize {
302+
// No answer for this
303+
return;
304+
}
305+
if cur_sum == target && cur_vec.len() == length as usize {
306+
// We get an answer
307+
ans.push(cur_vec.clone());
308+
return;
309+
}
310+
for i in cur_index..candidates.len() {
311+
cur_vec.push(candidates[i]);
312+
Self::dfs(target, length, i + 1, cur_sum + candidates[i], cur_vec, candidates, ans);
313+
cur_vec.pop().unwrap();
314+
}
315+
}
316+
}
317+
```
318+
286319
### **Go**
287320

288321
```go
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
impl Solution {
2+
#[allow(dead_code)]
3+
pub fn combination_sum3(k: i32, n: i32) -> Vec<Vec<i32>> {
4+
let mut ret = Vec::new();
5+
let mut candidates = (1..=9).collect();
6+
let mut cur_vec = Vec::new();
7+
Self::dfs(n, k, 0, 0, &mut cur_vec, &mut candidates, &mut ret);
8+
ret
9+
}
10+
11+
#[allow(dead_code)]
12+
fn dfs(target: i32, length: i32, cur_index: usize, cur_sum: i32, cur_vec: &mut Vec<i32>, candidates: &Vec<i32>, ans: &mut Vec<Vec<i32>>) {
13+
if cur_sum > target || cur_vec.len() > length as usize {
14+
// No answer for this
15+
return;
16+
}
17+
if cur_sum == target && cur_vec.len() == length as usize {
18+
// We get an answer
19+
ans.push(cur_vec.clone());
20+
return;
21+
}
22+
for i in cur_index..candidates.len() {
23+
cur_vec.push(candidates[i]);
24+
Self::dfs(target, length, i + 1, cur_sum + candidates[i], cur_vec, candidates, ans);
25+
cur_vec.pop().unwrap();
26+
}
27+
}
28+
}

0 commit comments

Comments
(0)

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