Skip to content

Commit bebf686

Browse files
aQuaaQua
aQua
authored and
aQua
committed
finish Problem 40
通过了测试。
1 parent 9564782 commit bebf686

File tree

5 files changed

+30
-18
lines changed

5 files changed

+30
-18
lines changed

Algorithms/0039.combination-sum/combination-sum.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,8 @@ func cs(candidates, solution []int, target int, result *[][]int) {
1919
*result = append(*result, solution)
2020
}
2121

22-
if target < 0 || len(candidates) == 0 || target < candidates[0] {
22+
if len(candidates) == 0 || target < candidates[0] {
2323
// target < candidates[0] 这个是因为candidates是排序好的
24-
// 否则,就应该是 target < min(candidates...)
25-
// 其实,不要这个判断条件,也能成功
2624
return
2725
}
2826

58.2 KB
Loading

Algorithms/0040.combination-sum-ii/README.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ Given a collection of candidate numbers (C) and a target number (T), find all un
66
Each number in C may only be used once in the combination.
77

88
Note:
9-
All numbers (including target) will be positive integers.
10-
The solution set must not contain duplicate combinations.
9+
1. All numbers (including target) will be positive integers.
10+
1. The solution set must not contain duplicate combinations.
1111
For example, given candidate set [10, 1, 2, 7, 6, 1, 5] and target 8,
1212
A solution set is:
1313
```
@@ -19,8 +19,8 @@ A solution set is:
1919
]
2020
```
2121
## 解题思路
22-
22+
这一题是[39. Combination Sum](https://leetcode.com/problems/combination-sum/)的变种。区别在于,元素不能重复使用。所以在划分的时候,对candidates的处理也不同。
2323

2424
## 总结
25-
26-
25+
又提交了一次,一不小心就迎来了人生的第一个100%,主要的功劳在leetcode服务器。
26+
![100%](100.png)

Algorithms/0040.combination-sum-ii/combination-sum-ii.go

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,20 +7,18 @@ func combinationSum2(candidates []int, target int) [][]int {
77

88
res := [][]int{}
99
solution := []int{}
10-
cs(candidates, solution, target, &res)
10+
cs2(candidates, solution, target, &res)
1111

1212
return res
1313
}
1414

15-
func cs(candidates, solution []int, target int, result *[][]int) {
15+
func cs2(candidates, solution []int, target int, result *[][]int) {
1616
if target == 0 {
1717
*result = append(*result, solution)
1818
}
1919

20-
if target < 0 || len(candidates) == 0 || target < candidates[0] {
21-
// target < candidates[0] 这个是因为candidates是排序好的
22-
// 否则,就应该是 target < min(candidates...)
23-
// 其实,不要这个判断条件,也能成功
20+
if len(candidates) == 0 || target < candidates[0] {
21+
// target < candidates[0] 因为candidates是排序好的
2422
return
2523
}
2624

@@ -29,7 +27,17 @@ func cs(candidates, solution []int, target int, result *[][]int) {
2927
// 可以注释掉以下语句,运行单元测试,查看错误发生。
3028
solution = solution[:len(solution):len(solution)]
3129

32-
cs(candidates, append(solution, candidates[0]), target-candidates[0], result)
30+
// 去掉已使用了的candidates[0]
31+
cs2(candidates[1:], append(solution, candidates[0]), target-candidates[0], result)
3332

34-
cs(candidates[1:], solution, target, result)
33+
// 不使用candidates[0]的话,就要把所有和candidates[0]相等的元素都去掉。
34+
cs2(next(candidates), solution, target, result)
35+
}
36+
37+
func next(candidates []int) []int {
38+
i := 0
39+
for i+1 < len(candidates) && candidates[i] == candidates[i+1] {
40+
i++
41+
}
42+
return candidates[i+1:]
3543
}

Algorithms/0040.combination-sum-ii/combination-sum-ii_test.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,13 +33,19 @@ func Test_Problem0039(t *testing.T) {
3333
question{
3434
para{[]int{10, 1, 2, 7, 6, 1, 5}, 8},
3535
ans{[][]int{
36-
[]int{1, 7},
36+
[]int{1, 1, 6},
3737
[]int{1, 2, 5},
38+
[]int{1, 7},
3839
[]int{2, 6},
39-
[]int{1, 1, 6},
4040
}},
4141
},
4242

43+
question{
44+
para{[]int{1}, 1},
45+
ans{[][]int{
46+
[]int{1},
47+
}},
48+
},
4349
// 如需多个测试,可以复制上方元素。
4450
}
4551

0 commit comments

Comments
 (0)