Skip to content

Commit e5dfcb1

Browse files
committed
update 39 40 Combination Sum II
1 parent 9f1e8b6 commit e5dfcb1

File tree

5 files changed

+100
-44
lines changed

5 files changed

+100
-44
lines changed

Backtracking/39_CombinationSum.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/*
22
* @Author: [email protected]
3-
* @Last Modified time: 2016-07-14 20:03:04
3+
* @Last Modified time: 2016-07-14 20:32:51
44
*/
55

66
class Solution {
@@ -9,7 +9,7 @@ class Solution {
99
Classic backtracking problem.
1010
1111
One key point: for one specified number,
12-
just scan the number larger than it to avoid duplicate combinations.
12+
just scan itself and numbers larger than it to avoid duplicate combinations.
1313
Besides, the current path need to be reset after dfs call in general.
1414
Refer to:
1515
https://discuss.leetcode.com/topic/23142/python-dfs-solution

Backtracking/39_CombinationSum.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ class Solution(object):
66
""" Classic backtracking problem.
77
88
One key point: for one specified number,
9-
just scan the number larger than it to avoid duplicate combinations.
9+
just scan itself and numbers larger than it to avoid duplicate combinations.
1010
Besides, the current path need to be reset after dfs call in general.
1111
Here we can just use `path + [num]` to avoid modifying path, so no need to reset.
1212
Refer to:

Backtracking/40_CombinationSumII.cpp

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/*
2+
* @Author: [email protected]
3+
* @Last Modified time: 2016-07-15 21:38:18
4+
*/
5+
6+
class Solution {
7+
/*
8+
Classic backtracking problem.
9+
10+
One key point: for one specified number,
11+
just scan the number larger than it to avoid duplicate combinations.
12+
Besides, the current path need to be reset after dfs call in general.
13+
That's saying, we need to do pop_back after push_back operator.
14+
*/
15+
private:
16+
void dfs_search(vector<int> &candidates, int start, int target, vector<int> &path, vector<vector<int>> &ans){
17+
if(target==0){
18+
ans.push_back(path);
19+
}
20+
for(int i=start; i<candidates.size(); i++){
21+
int num = candidates[i];
22+
if(num > target){
23+
return;
24+
}
25+
26+
// Here skip the same `adjacent` element to avoid duplicated.
27+
if(i > start && candidates[i] == candidates[i-1]){
28+
continue;
29+
}
30+
path.push_back(candidates[i]);
31+
dfs_search(candidates, i+1, target-num, path, ans);
32+
path.pop_back();
33+
}
34+
}
35+
public:
36+
vector<vector<int>> combinationSum2(vector<int>& candidates, int target) {
37+
if(candidates.size() == 0){
38+
return {};
39+
}
40+
sort(candidates.begin(), candidates.end());
41+
vector<int> path;
42+
vector<vector<int>> ans;
43+
dfs_search(candidates, 0, target, path, ans);
44+
return ans;
45+
}
46+
};
47+
48+
/*
49+
[]
50+
1
51+
[2, 5, 1, 4, 9]
52+
11
53+
[10, 1, 2, 7, 6, 1, 5]
54+
8
55+
*/

Backtracking/40_CombinationSumII.py

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
#! /usr/bin/env python
2+
# -*- coding: utf-8 -*-
3+
4+
5+
class Solution(object):
6+
""" Classic backtracking problem.
7+
8+
One key point: for one specified number,
9+
just scan the number larger than it to avoid duplicate combinations.
10+
Besides, the current path need to be reset after dfs call in general.
11+
Here we can just use `path + [num]` to avoid modifying path, so no need to reset.
12+
"""
13+
14+
def combinationSum2(self, candidates, target):
15+
if not candidates:
16+
return []
17+
candidates.sort()
18+
ans = []
19+
self.dfs_search(candidates, 0, target, [], ans)
20+
return ans
21+
22+
def dfs_search(self, candidates, start, target, path, ans):
23+
if target == 0:
24+
ans.append(path)
25+
for i in xrange(start, len(candidates)):
26+
num = candidates[i]
27+
if num > target:
28+
return
29+
# Here skip the same `adjacent` element to avoid duplicated.
30+
if i > start and candidates[i] == candidates[i - 1]:
31+
continue
32+
self.dfs_search(candidates, i + 1,
33+
target - num, path + [num], ans)
34+
35+
"""
36+
[]
37+
1
38+
[2, 5, 1, 4, 9]
39+
11
40+
[10, 1, 2, 7, 6, 1, 5]
41+
8
42+
"""

Week05/40.py

Lines changed: 0 additions & 41 deletions
This file was deleted.

0 commit comments

Comments
 (0)