forked from yanglei-github/Leetcode_in_python3
-
Notifications
You must be signed in to change notification settings - Fork 0
/
18_四数之和.py
80 lines (74 loc) · 2.5 KB
/
18_四数之和.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# -*- coding: utf-8 -*-
"""
Created on Fri Jun 12 09:47:23 2020
@author: leiya
"""
#排序加左右指针
class Solution:
def fourSum(self, nums: List[int], target: int) -> List[List[int]]:
n=len(nums)
if(not nums or n<4):
return []
nums.sort()
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):
#注意这里是大于i+1
if j > i+1 and nums[j]==nums[j-1]:
continue
L=j+1
R=n-1
while L<R:
if nums[i]+nums[j]+nums[L]+nums[R]==target:
res.append([nums[i],nums[j],nums[L],nums[R]])
while L<R and nums[L]==nums[L+1]:
L=L+1
while L<R and nums[R]==nums[R-1]:
R=R-1
L=L+1
R=R-1
elif nums[i]+nums[j]+nums[L]+nums[R]>target:
R=R-1
else:
L=L+1
return res
#剪枝后
class Solution:
def fourSum(self, nums: List[int], target: int) -> List[List[int]]:
n=len(nums)
if(not nums or n<4):
return []
nums.sort()
res=[]
for i in range(n-3):
if nums[i]+nums[i+1]+nums[i+2]+nums[i+3]>target:
break
if nums[i]+nums[-1]+nums[-2]+nums[-3]<target:
continue
if i>0 and nums[i]==nums[i-1]:
continue
for j in range(i+1,n-2):
if nums[i]+nums[j]+nums[j+1]+nums[j+2]>target:
break
if nums[i]+nums[j]+nums[-1]+nums[-2]<target:
continue
if j-i>1 and nums[j]==nums[j-1]:
continue
L=j+1
R=n-1
while L<R:
if nums[i]+nums[j]+nums[L]+nums[R]==target:
res.append([nums[i],nums[j],nums[L],nums[R]])
while L<R and nums[L]==nums[L+1]:
L=L+1
while L<R and nums[R]==nums[R-1]:
R=R-1
L=L+1
R=R-1
elif nums[i]+nums[j]+nums[L]+nums[R]>target:
R=R-1
else:
L=L+1
return res