给定一个数组,选择三个元素相加,结果为target,找出全部符合的三元组git
Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0github
Find all unique triplets in the array which gives the sum of zero.数组
Note: The solution set must not contain duplicate triplets.app
For example, given array S = [-1, 0, 1, 2, -1, -4]指针
example 1code
input: [-1, 0, 1, 2, -1, -4] A solution set is: [ [-1, 0, 1], [-1, -1, 2] ]
乱序数组,须要找到全部组合,须要三层循环,复杂度为O(n³)。排序
能够先排序,排序后只须要两层循环,复杂度为O(n²)。第一层循环遍历全部元素,第二层循环因为数组已经排序,只须要头尾两个指针像中间靠拢,一但三个元素相加为target,则添加此三元组,而后继续像中间靠拢扫描。three
须要避免重复的三元组被加入ip
class Solution(object): def threeSum(self, nums): """ :type nums: List[int] :rtype: List[List[int]] """ nums.sort() ret = [] for i in range(len(nums) - 2): # 避免重复 if i > 0 and nums[i] == nums[i-1]: continue j, k = i + 1, len(nums) - 1 while j < k: if nums[i] + nums[j] + nums[k] == 0: ret.append([nums[i], nums[j], nums[k]]) j += 1 k -= 1 # 避免重复 while j < k and nums[j] == nums[j - 1]: j += 1 while j < k and nums[k] == nums[k + 1]: k -= 1 elif nums[i] + nums[j] + nums[k] < 0: j += 1 else: k -= 1 return ret
本题以及其它leetcode题目代码github地址: github地址element