-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path3Sum
More file actions
53 lines (34 loc) · 1.51 KB
/
Copy path3Sum
File metadata and controls
53 lines (34 loc) · 1.51 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
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
class Solution:
def threeSum(self, nums: List[int]) -> List[List[int]]:
nums.sort()
# print(nums)
ans = []
for c1 in range(len(nums)-2):
n1 = nums[c1]
c2 = c1+1
n2 = nums[c2]
c3 = len(nums)-1
n3 = nums[c3]
if n1>0:
return ans
if c1>0 and (nums[c1] == nums[c1-1]):
continue
while c2 < c3:
# print(nums[c1] + nums[c2]+nums[c3])
if (nums[c1] + nums[c2]+nums[c3]) == 0:
ans.append([nums[c1],nums[c2],nums[c3]])
while c3>0 and (nums[c3-1] == nums[c3]):
c3-=1
c3-=1
while (c2< (len(nums)-1)) and (nums[c2+1] == nums[c2]):
c2+=1
c2+=1
elif (nums[c1] + nums[c2]+nums[c3]) > 0:
while c3 >0 and nums[c3-1] == nums[c3]:
c3-=1
c3-=1
elif (nums[c1] + nums[c2]+nums[c3]) < 0:
while c2 < len(nums)-1 and nums[c2+1] == nums[c2]:
c2+=1
c2+=1
return ans