1. 1#MEDUIUM 53Sum
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]
]
思路:先用自带排序功能,给数组排序,然后从头到尾遍历,需要的剩余值,就在i+1到length-1这个范围内找,注意这里有两处要紧的要处理防止重复!!从头到尾遍历时需要看看是不是和前面的值相等了,在后面的范围搜索时,也要时刻关注是不是和下一个值一样的。如果和大了就High--.如果和小了就Low++
错误:没有防止重复的处理能力!
网友评论