-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLeetcode 04.py
More file actions
48 lines (27 loc) · 829 Bytes
/
Copy pathLeetcode 04.py
File metadata and controls
48 lines (27 loc) · 829 Bytes
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
# Leetcode Question 04 (Four sum)
def foursum(self, nums: list-> list[int]):
nums.sort()
answer = []
for i in range(0 len(nums)-3):
if i>0 and nums[i] == nums[i-1]:
continue
for l in range(i+1, len(nums)-2):
if l>i+1 and nums[l]==nums[l-1]:
continue
j = i+1
k = len(nums)-1
while j<k:
sum = nums[i]+nums[j]+nums[k]
if sum == target:
answer. append([nums[i],nums[j],nums[k]])
j+=1
k-=1
while j<k and nums[j]==nums[j-1]:
j+=1
while j<k and nums[k]==nums[k+1]:
k-=1
elif sum>target:
k-=1
else:
j+=1
return answer