-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path4sum.py
More file actions
27 lines (27 loc) · 1.01 KB
/
Copy path4sum.py
File metadata and controls
27 lines (27 loc) · 1.01 KB
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
class Solution:
def fourSum(self, nums: List[int], target: int) -> List[List[int]]:
nums.sort()
a = []
for i in range(len(nums) - 3):
if i > 0 and nums[i] == nums[i - 1]:
continue
for j in range(i + 1, len(nums) - 2):
if j > i + 1 and nums[j] == nums[j - 1]:
continue
l = j + 1
r = len(nums) - 1
while l < r:
total = nums[i]+nums[j]+nums[l]+nums[r]
if target == total:
a.append([nums[i],nums[j],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 total < target:
l += 1
else:
r -= 1
return a