Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add rust solution to lc problem: No.0216 #1374

Merged
merged 1 commit into from
Aug 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions solution/0200-0299/0216.Combination Sum III/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -326,6 +326,39 @@ public:
};
```

### **Rust**

```rust
impl Solution {
#[allow(dead_code)]
pub fn combination_sum3(k: i32, n: i32) -> Vec<Vec<i32>> {
let mut ret = Vec::new();
let mut candidates = (1..=9).collect();
let mut cur_vec = Vec::new();
Self::dfs(n, k, 0, 0, &mut cur_vec, &mut candidates, &mut ret);
ret
}

#[allow(dead_code)]
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>>) {
if cur_sum > target || cur_vec.len() > length as usize {
// No answer for this
return;
}
if cur_sum == target && cur_vec.len() == length as usize {
// We get an answer
ans.push(cur_vec.clone());
return;
}
for i in cur_index..candidates.len() {
cur_vec.push(candidates[i]);
Self::dfs(target, length, i + 1, cur_sum + candidates[i], cur_vec, candidates, ans);
cur_vec.pop().unwrap();
}
}
}
```

### **Go**

```go
Expand Down
33 changes: 33 additions & 0 deletions solution/0200-0299/0216.Combination Sum III/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,39 @@ public:
};
```

### **Rust**

```rust
impl Solution {
#[allow(dead_code)]
pub fn combination_sum3(k: i32, n: i32) -> Vec<Vec<i32>> {
let mut ret = Vec::new();
let mut candidates = (1..=9).collect();
let mut cur_vec = Vec::new();
Self::dfs(n, k, 0, 0, &mut cur_vec, &mut candidates, &mut ret);
ret
}

#[allow(dead_code)]
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>>) {
if cur_sum > target || cur_vec.len() > length as usize {
// No answer for this
return;
}
if cur_sum == target && cur_vec.len() == length as usize {
// We get an answer
ans.push(cur_vec.clone());
return;
}
for i in cur_index..candidates.len() {
cur_vec.push(candidates[i]);
Self::dfs(target, length, i + 1, cur_sum + candidates[i], cur_vec, candidates, ans);
cur_vec.pop().unwrap();
}
}
}
```

### **Go**

```go
Expand Down
28 changes: 28 additions & 0 deletions solution/0200-0299/0216.Combination Sum III/Solution.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
impl Solution {
#[allow(dead_code)]
pub fn combination_sum3(k: i32, n: i32) -> Vec<Vec<i32>> {
let mut ret = Vec::new();
let mut candidates = (1..=9).collect();
let mut cur_vec = Vec::new();
Self::dfs(n, k, 0, 0, &mut cur_vec, &mut candidates, &mut ret);
ret
}

#[allow(dead_code)]
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>>) {
if cur_sum > target || cur_vec.len() > length as usize {
// No answer for this
return;
}
if cur_sum == target && cur_vec.len() == length as usize {
// We get an answer
ans.push(cur_vec.clone());
return;
}
for i in cur_index..candidates.len() {
cur_vec.push(candidates[i]);
Self::dfs(target, length, i + 1, cur_sum + candidates[i], cur_vec, candidates, ans);
cur_vec.pop().unwrap();
}
}
}