Given an array nums
of n integers, are there elements a, b, c in nums
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.
Example:
Given array nums = [-1, 0, 1, 2, -1, -4], A solution set is: [ [-1, 0, 1], [-1, -1, 2] ]
nums
,判断
nums
中是否存在三个元素
a,b,c ,
使得
a + b + c =
0 ?找出所有满足条件且不重复的三元组。
挺有趣的一题,反复确认了一下,题目没有时间复杂度的要求,那么三层循环依次寻找就成为了最简单的写法了,有兴趣的同学可以自己写写看,这里就不写了。虽然题目没有要求,但是身为程序猿好歹还是要有点极客精神,单纯的使用枚举法就有点low了。
观察一下数组,发现例子给的是无序数组,但是假如我们把数组变为有序确可以帮助我们优化算法,那么就假定第一步是对数组进行排序。
//不想写排序算法的同学可以这样。
//Arrays.sort(nums);
public void sort(int[] nums, int left, int right){
if (left > right)
return;
int tempLeft = left;
int tempRight = right;
int x = nums[left];
while (left < right){
while (left<right && x < nums[right])
right--;
if (left < right)
nums[left++] = nums[right];
while (left<right && x > nums[left])
left++;
if (left < right)
nums[right--] = nums[left];
}
nums[left] = x;
sort(nums, tempLeft, tempRight - 1);
sort(nums, tempLeft + 1, tempRight);
}
排序之后,以上面那个例子为例
nums = [-4,-1,-1,0, 1, 2]
如nums[0]+nums[1]+nums[5]<0那么我们明确知道nums[5]前面的都比他小,则本次循环可以跳出了
如nums[1]+nums[2]+nums[4]==0,那么还需要继续往前寻找,直到出现上述情况。
public List<List<Integer>> threeSum(int[] nums) {
sort(nums, 0, nums.length - 1);
List<List<Integer>> resultList = new ArrayList<>();
for (int i = 0; i < nums.length; i++) {
int left = i + 1;
int right = nums.length - 1;
while (left < right){
if (nums[i] + nums[left] + nums[right] == 0){
resultList.add(Arrays.asList(nums[i], nums[left], nums[right]));
right--;
}else if (nums[i] + nums[left] + nums[right] < 0){
left++;
}else {
right--;
}
}
}
return resultList;
}
写完了上面的代码运行,发现和例子中的结果不一样,例子中还需要去重。加上去掉重复数据的代码。其实也可以得到resultList之后再进行去重,但是如此就多进行了循环,所以需要在循环的过程中就去掉重复数据。
public List<List<Integer>> threeSum(int[] nums) {
sort(nums, 0, nums.length - 1);
List<List<Integer>> resultList = new ArrayList<>();
for (int i = 0; i < nums.length - 2; i++) {
int left = i + 1;
int right = nums.length - 1;
while (left < right){
if (nums[i] + nums[left] + nums[right] == 0){
resultList.add(Arrays.asList(nums[i], nums[left], nums[right]));
right--;
left++;
while (left < right && nums[left] == nums[left - 1])
left++;
while (left < right && nums[right] == nums[right + 1])
right--;
}else if (nums[i] + nums[left] + nums[right] < 0){
left++;
}else {
right--;
}
}
while (i < nums.length - 2 && nums[i] == nums[i+1])
i++;
}
return resultList;
}
在LeetCode提交过程中出现了一件很尴尬的事情,如果对JDK源码熟悉的同学应该已经发现了,我特么写的快排在复杂数据面前并没有Arrays.sort()来的效率高,导致提交的时候出现了超时,很尴尬啊。
简单看了下Arrays.sort()源码
static void sort(int[] a, int left, int right,
int[] work, int workBase, int workLen) {
// Use Quicksort on small arrays
if (right - left < QUICKSORT_THRESHOLD) {
sort(a, left, right, true);
return;
}
....
private static void sort(int[] a, int left, int right, boolean leftmost) {
int length = right - left + 1;
// Use insertion sort on tiny arrays
if (length < INSERTION_SORT_THRESHOLD) {
if (leftmost) {
JDK中根据数组的长度采用了快排、插入、等多种排序方法,血的教训告诉我们,实际开发过程中尽量避免重复的造轮子,当然学习的过程中就不要怕犯错了。