-
Notifications
You must be signed in to change notification settings - Fork 1
/
4sum.py
55 lines (48 loc) · 1.63 KB
/
4sum.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
"""
Given an array S of n integers, are there elements a, b, c, and d in S such that a + b + c + d = target? Find all unique quadruplets in the array which gives the sum of target.
Note: The solution set must not contain duplicate quadruplets.
For example, given array S = [1, 0, -1, 0, -2, 2], and target = 0.
A solution set is:
[
[-1, 0, 0, 1],
[-2, -1, 1, 2],
[-2, 0, 0, 2]
]
"""
class ListNode(object):
def __init__(self, x=None):
self.val = x
self.next = None
class Solution(object):
def fourSum(self, nums, target):
"""
:type nums: List[int]
:type target: int
:rtype: List[List[int]]
"""
nums = sorted(nums)
self.results = []
self.findSum(nums, target, 4, [])
return self.results
def findSum(self, nums, target, N, result):
if N == 2:
l, r = 0, len(nums) - 1
while l < r:
if nums[l] + nums[r] == target:
self.results.append(result+[nums[l], nums[r]])
l += 1
r -= 1
while l < r and nums[l] == nums[l - 1]:
l += 1
while l < r and nums[r] == nums[r + 1]:
r -= 1
elif nums[l] + nums[r] < target:
l += 1
else:
r -= 1
else:
for i in range(len(nums) - (N - 1)):
if i == 0 or i > 0 and nums[i-1] != nums[i]:
self.findSum(nums[i+1:], target - nums[i], N - 1, result + [nums[i]])
t = [1, 0, -1, 0, -2, 2]
r = Solution().fourSum(t, 0)