-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path15.3-sum.py
39 lines (31 loc) · 932 Bytes
/
15.3-sum.py
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
#
# @lc app=leetcode id=15 lang=python3
#
# [15] 3Sum
#
# @lc code=start
class Solution:
def threeSum(self, nums: List[int]) -> List[List[int]]:
nums.sort()
rtn = []
for i in range(len(nums)-2):
if i > 0 and nums[i] == nums[i-1]:
continue
lp, rp = i+1, len(nums)-1
target = 0 - nums[i]
while lp < rp:
cur = nums[lp] + nums[rp]
if cur == target:
rtn.append([nums[i], nums[lp], nums[rp]])
while lp < rp and nums[lp] == nums[lp + 1]:
lp += 1
while lp < rp and nums[rp] == nums[rp-1]:
rp -= 1
lp += 1
rp -= 1
elif cur < target:
lp += 1
else:
rp -= 1
return rtn
# @lc code=end