-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcombination sum.py
33 lines (29 loc) · 1.04 KB
/
combination sum.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
__author__ = 'yuyue'
class Solution(object):
def combinationSum(self, candidates, target):
tempresult = []
result = []
flag=[0]
candidates = sorted(candidates)
length = len(candidates)
def dfs(pos, candidates, length, target, result, tempresult,flag):
tempresult.append(candidates[pos])
if candidates[pos] == target:
result.append(tempresult[:])
tempresult.pop()
return
elif candidates[pos] > target:
tempresult.pop()
flag[0] = 1
return
else:
for i in range(pos, length):
dfs(i, candidates, length, target - candidates[pos], result, tempresult,flag)
if flag[0]:
flag[0] = 0
break
tempresult.pop()
return
for i in range(length):
dfs(i,candidates,length,target,result,tempresult,flag)
return result