leetcode_15 3Sum(TwoPointers)
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了leetcode_15 3Sum(TwoPointers)相关的知识,希望对你有一定的参考价值。
Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? Find all unique triplets in the array which gives the sum of zero.
Note: The solution set must not contain duplicate triplets.
For example, given array S = [-1, 0, 1, 2, -1, -4], A solution set is: [ [-1, 0, 1], [-1, -1, 2] ]
易错:出现重复的结果
暴力枚举三个数复杂度为O(N^3)。先考虑2Sum的做法,假设升序数列a,对于一组解ai,aj, 另一组解ak,al ,必然满足 i<k j>l 或 i>k j<l, 因此我们可以用两个指针,初始时指向数列两端
指向数之和大于目标值时,右指针向左移使得总和减小,反之左指针向右移。由此可以用O(N)的复杂度解决2Sum问题,3Sum则枚举第一个数O(N^2),使用有序数列的好处是,在枚举和移动指针时值相等的数可以跳过,省去去重部分
Python实现:
class Solution(object): def threeSum(self, nums): nums.sort() ans = [] # 结果 for i in range(0, len(nums)-2): if i and nums[i] == nums[i-1]: continue target = -1*nums[i] left = i + 1 right = len(nums)-1 while left < right: if nums[left] + nums[right] == target: ans.append([nums[i], nums[left], nums[right]]) left += 1 right -= 1 while left < right and nums[left] == nums[left-1]: #避免结果重复 !!! left += 1 while left < right and nums[right] == nums[right+1]: right -= 1 elif nums[left] + nums[right] < target: left += 1 # while left < right and nums[left] == nums[left+1]: # left += 1 else: right -= 1 # while left < right and nums[right] == nums[right-1]: # right -= 1 return ans
以上是关于leetcode_15 3Sum(TwoPointers)的主要内容,如果未能解决你的问题,请参考以下文章