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