-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path15.py
More file actions
executable file
·30 lines (30 loc) · 1.09 KB
/
Copy path15.py
File metadata and controls
executable file
·30 lines (30 loc) · 1.09 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
class Solution:
def threeSum(self, nums: List[int]) -> List[List[int]]:
l2 = []
nums.sort()
for a in range(len(nums)):
if a != 0 and nums[a] == nums[a-1]:
continue
i = a + 1 # 左指针
j = len(nums) - 1 # 右指针
for b in range(len(nums)):
if i >= j: # 左右指针不应该相碰
break
if i != a + 1 and nums[i] == nums[i - 1]: # 第二个数也不应该出现重复的情况
i += 1
continue
if nums[a] + nums[i] + nums[j] == 0:
stack = []
stack.append(nums[a])
stack.append(nums[i])
stack.append(nums[j])
l2.append(stack)
i += 1
continue
if nums[a] + nums[i] + nums[j] < 0:
i += 1
continue
if nums[a] + nums[i] + nums[j] > 0:
j -= 1
continue
return l2