Skip to content

Commit 7802a1e

Browse files
authored
Update combination-sum-iii.py
1 parent ea3c9ef commit 7802a1e

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed

Python/combination-sum-iii.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,3 +43,24 @@ def combinationSumRecu(self, result, intermediate, start, k, target):
4343
self.combinationSumRecu(result, intermediate, start + 1, k - 1, target - start)
4444
intermediate.pop()
4545
start += 1
46+
47+
48+
# OR
49+
class Solution2:
50+
def combinationSum3(self, k, n):
51+
"""
52+
:type k: int
53+
:type n: int
54+
:rtype: List[List[int]]
55+
"""
56+
res = []
57+
self.dfs(range(1,10), k, n, 0, [], res)
58+
return res
59+
60+
def dfs(self, nums, k, n, index, path, res):
61+
if k < 0 or n < 0:
62+
return
63+
if k == 0 and n == 0:
64+
res.append(path)
65+
for i in range(index, len(nums)):
66+
self.dfs(nums, k-1, n-nums[i], i+1, path+[nums[i]], res)

0 commit comments

Comments
 (0)