-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathk_sum_2.py
47 lines (40 loc) · 1.17 KB
/
k_sum_2.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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
import unittest
from typing import List
# 这题与combination_sum很相似
def k_sum_2(nums: List[int], k: int, target: int) -> List[List[int]]:
size = len(nums)
if size == 0:
return []
results = []
# 题目给定n个不同的正整数,所以不需要去重
nums = sorted(nums)
dfs(target, k, nums, 0, size, [], results)
return results
def dfs(
target: int,
k: int,
nums: List[int],
nums_start: int,
size: int,
combination: List[int],
results: List[List[int]]
) -> None:
if k == 0:
if target == 0:
results.append(combination.copy())
return
for i in range(nums_start, size):
residue = target - nums[i]
if residue < 0:
break
combination.append(nums[i])
dfs(target - nums[i], k - 1, nums, i + 1, size, combination, results)
combination.pop()
class Testing(unittest.TestCase):
TEST_CASES = [
([1, 2, 3, 4], 2, 5, [[1, 4], [2, 3]]),
([1, 3, 4, 6], 3, 8, [[1, 3, 4]]),
]
def test(self):
for nums, k, target, results in self.TEST_CASES:
self.assertCountEqual(results, k_sum_2(nums, k, target))