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 d95cf01

Browse files
Merge pull request youngyangyang04#1523 from cezarbbb/Backtracking
添加 0039.组合总和 0040.组合总和II Rust版本
2 parents 64c4d9b + 57840ef commit d95cf01

File tree

2 files changed

+64
-0
lines changed

2 files changed

+64
-0
lines changed

‎problems/0039.组合总和.md‎

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -417,6 +417,35 @@ function combinationSum(candidates: number[], target: number): number[][] {
417417
};
418418
```
419419

420+
## Rust
421+
422+
```Rust
423+
impl Solution {
424+
pub fn backtracking(result: &mut Vec<Vec<i32>>, path: &mut Vec<i32>, candidates: &Vec<i32>, target: i32, mut sum: i32, start_index: usize) {
425+
if sum == target {
426+
result.push(path.to_vec());
427+
return;
428+
}
429+
for i in start_index..candidates.len() {
430+
if sum + candidates[i] <= target {
431+
sum += candidates[i];
432+
path.push(candidates[i]);
433+
Self::backtracking(result, path, candidates, target, sum, i);
434+
sum -= candidates[i];
435+
path.pop();
436+
}
437+
}
438+
}
439+
440+
pub fn combination_sum(candidates: Vec<i32>, target: i32) -> Vec<Vec<i32>> {
441+
let mut result: Vec<Vec<i32>> = Vec::new();
442+
let mut path: Vec<i32> = Vec::new();
443+
Self::backtracking(&mut result, &mut path, &candidates, target, 0, 0);
444+
result
445+
}
446+
}
447+
```
448+
420449
## C
421450

422451
```c

‎problems/0040.组合总和II.md‎

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -599,6 +599,41 @@ function combinationSum2(candidates: number[], target: number): number[][] {
599599
};
600600
```
601601

602+
## Rust
603+
604+
```Rust
605+
impl Solution {
606+
pub fn backtracking(result: &mut Vec<Vec<i32>>, path: &mut Vec<i32>, candidates: &Vec<i32>, target: i32, mut sum: i32, start_index: usize, used: &mut Vec<bool>) {
607+
if sum == target {
608+
result.push(path.to_vec());
609+
return;
610+
}
611+
for i in start_index..candidates.len() {
612+
if sum + candidates[i] <= target {
613+
if i > 0 && candidates[i] == candidates[i - 1] && used[i - 1] == false { continue; }
614+
sum += candidates[i];
615+
path.push(candidates[i]);
616+
used[i] = true;
617+
Self::backtracking(result, path, candidates, target, sum, i + 1, used);
618+
used[i] = false;
619+
sum -= candidates[i];
620+
path.pop();
621+
}
622+
}
623+
}
624+
625+
pub fn combination_sum2(candidates: Vec<i32>, target: i32) -> Vec<Vec<i32>> {
626+
let mut result: Vec<Vec<i32>> = Vec::new();
627+
let mut path: Vec<i32> = Vec::new();
628+
let mut used: Vec<bool> = vec![false; candidates.len()];
629+
let mut candidates = candidates;
630+
candidates.sort();
631+
Self::backtracking(&mut result, &mut path, &candidates, target, 0, 0, &mut used);
632+
result
633+
}
634+
}
635+
```
636+
602637
## C
603638

604639
```c

0 commit comments

Comments
(0)

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