-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path3Sum.py
28 lines (26 loc) · 839 Bytes
/
3Sum.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
#! /usr/bin/env python3
# -*- coding: utf-8 -*-
# Source: https://leetcode.com/problems/3sum/
# Author: Miao Zhang
# Date: 2021-01-05
class Solution:
def threeSum(self, nums: List[int]) -> List[List[int]]:
nums.sort()
n = len(nums)
res = []
for i in range(n - 2):
if i > 0 and nums[i] == nums[i - 1]:
continue
target = -1 * nums[i]
j, k = i + 1, n - 1
while j < k:
if nums[j] + nums[k] == target:
res.append([nums[i], nums[j], nums[k]])
j += 1
while j < k and nums[j - 1] == nums[j]:
j += 1
elif nums[j] + nums[k] > target:
k -= 1
else:
j += 1
return res