-
Notifications
You must be signed in to change notification settings - Fork 1
/
nsum.py
74 lines (72 loc) · 2.31 KB
/
nsum.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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#coding=utf-8
class Solution(object):
def nsum(self, N, nums, target):
"""
:type N, int
:type nums: List[int]
:type target int
:rtype: List[List[int]]
"""
length = len(nums)
if N == 0 :
return []
if length < N:
return []
# 假设nums已经排好序了
if nums[0] * N > target:
return []
if nums[-1] * N < target:
return []
if N == 1:
for i in range(length):
if nums[i] == target:
return [nums[i]]
return []
elif N == 2:
begin, end = 0, length - 1
result = []
while begin < end:
two_sum = nums[begin] + nums[end]
if two_sum > target:
end -= 1
elif two_sum < target:
begin += 1
else:
result.append([nums[begin], nums[end]])
while begin < end and nums[begin] == nums[begin+1]:
begin += 1
while begin < end and nums[end] == nums[end-1]:
end -= 1
begin += 1
end -= 1
return result
else:
results = []
for i in range(length-N+1):
# 如果第一个数就大,后面的和肯定更大,最后一个同理
if nums[i] * N > target or nums[-1] * N < target:
break
# 第一个数字不能有重复的
if i > 0 and nums[i] == nums[i-1]:
continue
# 先取出n-1sum的列表
result_n_1 = self.nsum(N-1, nums[i+1:], target-nums[i])
if result_n_1:
for element in result_n_1:
result = [nums[i]] + element
results.append(result)
return results
def four_sum(self, nums, target):
"""
:type nums: List[int]
:type target int
:rtype: List[List[int]]
"""
# 先进行排序
nums.sort()
return self.nsum(4, nums, target)
if __name__ == '__main__':
o = Solution()
nums = [-1,2,2,-5,0,-1,4]
nums.sort()
print o.nsum(4, nums, 3)