Skip to content

Commit 09439c2

Browse files
committed
committed from zkp
1 parent 25f0958 commit 09439c2

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

LeetCode/threeSum.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import bisect
2+
class Solution:
3+
def threeSum(self, nums):
4+
"""
5+
:type nums: List[int]
6+
:rtype: List[List[int]]
7+
"""
8+
nums.sort()
9+
res = []
10+
t = bisect.bisect_right(nums, 0)
11+
for i in range(t):
12+
if i == 0 or nums[i] >nums[i-1]:
13+
l = i+1
14+
r = len(nums) -1
15+
while l < r:
16+
A = nums[i] + nums[l] + nums[r]
17+
if A == 0:
18+
res.append([nums[i], nums[l], nums[r]])
19+
l += 1
20+
r -= 1
21+
while l < r and nums[l] == nums[l-1]:
22+
l += 1
23+
while r > l and nums[r+1] == nums[r]:
24+
r -= 1
25+
elif A >0:
26+
r -= 1
27+
elif A < 0:
28+
l += 1
29+
return res

0 commit comments

Comments
 (0)