forked from fantastic-Feifei/Leetcode_Solutions_python3
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path15_threeSum.py
More file actions
executable file
·43 lines (26 loc) · 1.18 KB
/
15_threeSum.py
File metadata and controls
executable file
·43 lines (26 loc) · 1.18 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
class Solution:
def threeSum(self, nums: [int]) -> [[int]]:
length = len(nums)
if(not nums or length < 3): return []
res = []
nums.sort()
for i in range(length):
if(nums[i] > 0): return res
if(i > 0 and nums[i] == nums[i - 1]): continue
#👆这个i > 0啊 学问可大了呢, nums=[0, 0, 0]的时候
#为什么要continue呢, 如果前后两位数一样,答案可能会出现两对重复值
left, right = i + 1, length - 1
while(left < right):
if(nums[i] + nums[left] + nums[right] == 0):
res.append([nums[i], nums[left], nums[right]])
while(left < right and nums[left] == nums[left + 1]): left = left + 1
while(left < right and nums[right] == nums[right - 1]): right = right - 1
left = left + 1
right = right - 1
elif(nums[i] + nums[left] + nums[right] < 0): left = left + 1
else: right = right - 1
return res
nums = [-1,0,1,2,-1,-4]
ob = Solution()
ans = ob.threeSum(nums)
ans