-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy path15.3-sum.py
57 lines (51 loc) · 1.56 KB
/
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#
# @lc app=leetcode id=15 lang=python3
#
# [15] 3Sum
#
# @lc code=start
# TAGS: Array, Two Pointers
class Solution:
# 724 ms, 86.78%. Time and Space: O(N^2)
def threeSum(self, nums: List[int]) -> List[List[int]]:
nums.sort()
ans = set()
for i, first in enumerate(nums):
# Skip obvious cases
if first > 0:
break
if i and first == nums[i-1]:
continue
# Essentially 2 sum problem
seen = {}
for second in nums[i + 1:]:
if second in seen:
ans.add(seen[second] + (second,))
seen[- first - second] = (first, second)
return ans
# 660 ms, 94.66%. Time: O(N^2), Space: O(N^2)
def threeSum(self, nums: List[int]) -> List[List[int]]:
rv = []
nums.sort()
for i, num in enumerate(nums[:-2]):
if num > 0:
break
if i > 0 and num == nums[i-1]:
continue
l, r = i + 1, len(nums) - 1
while l < r:
total = num + nums[l] + nums[r]
if total < 0:
l += 1
elif total > 0:
r -= 1
else:
rv.append([num, nums[l], nums[r]])
while l < r and nums[l] == nums[l + 1]:
l += 1
while l < r and nums[r] == nums[r - 1]:
r -= 1
l += 1
r -= 1
return rv
# @lc code=end