【leetcode】18. 四数之和
排序+双指针(优化前)
class Solution(object):
def fourSum(self, nums, target):
"""
:type nums: List[int]
:type target: int
:rtype: List[List[int]]
"""
res = list()
length = len(nums)
if length < 4:
return res
nums.sort()
for i in range(length):
if i > 0 and nums[i] == nums[i - 1]:
continue
j = i + 1
while j < length:
while i + 1 < j < length and nums[j] == nums[j - 1]:
j += 1
L = j + 1
R = length - 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 += 1
while L < R and nums[R] == nums[R - 1]:
R -= 1
L += 1
R -= 1
elif nums[i] + nums[j] + nums[L] + nums[R] < target:
L += 1
else:
R -= 1
j += 1
return res
排序+双指针(优化后)
class Solution(object):
def fourSum(self, nums, target):
"""
:type nums: List[int]
:type target: int
:rtype: List[List[int]]
"""
res = list()
length = len(nums)
if length < 4:
return res
nums.sort()
for i in range(length - 3):
if i > 0 and nums[i] == nums[i - 1]:
continue
if nums[i] + nums[i + 1] + nums[i + 2] + nums[i + 3] > target:
break
if nums[i] + nums[length - 3] + nums[length - 2] + nums[length - 1] < target:
continue
for j in range(i + 1, length - 2):
if j > i + 1 and nums[j] == nums[j - 1]:
continue
if nums[i] + nums[j] + nums[j + 1] + nums[j + 2] > target:
break
if nums[i] + nums[j] + nums[length - 2] + nums[length - 1] < target:
continue
L = j + 1
R = length - 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 += 1
while L < R and nums[R] == nums[R - 1]:
R -= 1
L += 1
R -= 1
elif nums[i] + nums[j] + nums[L] + nums[R] < target:
L += 1
else:
R -= 1
return res
总结
1、该题目为15、16题的结合,只需在“三数之和”外层再添加一层循环即可。
但是,我们发现该方法的运行时间很长,时长达到“840ms”,需要优化。
2、在循环体内,添加判断语句:
if nums[i] + nums[i + 1] + nums[i + 2] + nums[i + 3] > target:
break
if nums[i] + nums[length - 3] + nums[length - 2] + nums[length - 1] < target:
continue
if nums[i] + nums[j] + nums[j + 1] + nums[j + 2] > target:
break
if nums[i] + nums[j] + nums[length - 2] + nums[length - 1] < target:
continue
以此减少循环的次数。经过优化,时长减少至“44ms”。