package com.ytx.hash;
import java.util.ArrayList;
import java.util.Arrays;
/** 题目:3sum
*
* 描述: 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:
Elements in a triplet (a,b,c) must be in non-descending order. (ie, a ≤ b ≤ c)
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)
*
*
* @author yuantian xin
*
*
* public class ThreeSumSolution2 {
private ArrayList<ArrayList<Integer>> list;
public ArrayList<ArrayList<Integer>> threeSum(int[] num) {
list = new ArrayList<ArrayList<Integer>>();
Arrays.sort(num);
int i = 0;
for (i = 0; i <= num.length - 3; i++) {
if (i != 0 && num[i] == num[i - 1]) {
continue;
}
judgeAndPut(num, i, i + 1, num.length - 1);
}
return list;
}
private void judgeAndPut(int[] num, int i, int p, int q) {
while (p < q) {
if (num[p] + num[q] < -num[i]) {
p++;
} else if (num[p] + num[q] > -num[i]){
q--;
} else if (num[p] + num[q] == -num[i]) {
ArrayList<Integer> tmpList = new ArrayList<Integer>();
tmpList.add(num[i]);
tmpList.add(num[p]);
tmpList.add(num[q]);
list.add(tmpList);
p++;
q--;
while (p < q && num[p] == num[p - 1]) {
p++;
}
while (p < q && num[q] == num[q + 1]) {
q--;
}
}
}
}
这道题用java写,思路和写法都没错,但是会超时,提交总是超时。用C++写秒过:
class Solution {
public:
vector<vector<int> > threeSum(vector<int> &num) {
vector<vector<int> > result;
int len = num.size();
if(len<3) return result;
sort(num.begin(), num.end());
vector<int> tmp(3);
for(int i=0; i<len; i++){
//防止重复,防止与之前的重复
if(i == 0 || num[i] != num[i-1]){
int left = i+1;
int right = len-1;
while(left < right){
while(left<right && num[i] + num[left] + num[right] > 0) right--;
if(left<right && num[i] + num[left] + num[right] == 0){
tmp[0] = num[i];
tmp[1] = num[left];
tmp[2] = num[right];
result.push_back(tmp);
while(left<right && num[left] == tmp[1]) left++; //很关键,可以排除之后的重复
}else left++;
}
}
}
return result;
}
};
*
*/
public class Three_sum {
public ArrayList<ArrayList<Integer>> threeSum(int[] num) {
int len = num.length;
ArrayList<ArrayList<Integer>> res = new ArrayList<ArrayList<Integer>>();
if (num == null || len < 3) return res;
Arrays.sort(num);//升序排序
for(int i = 0; i < len - 2; i++) {
if(num[i] > 0) break;
//防止重复
if(i == 0 || num[i] > num[i-1]) {
int left = i+ 1;
int right =len - 1;
while(left < right) {
while(left < right && num[i] + num[left] + num[right] > 0) right--;
if(left < right && num[i] + num[left] + num[right] == 0) {
ArrayList<Integer> temp = new ArrayList<Integer>();
temp.add(num[i]);
temp.add(num[left]);
temp.add(num[right]);
res.add(temp);
while(left<right && num[left] == temp.get(1)) left++;
}else {
left++;
}
}
}
}
return res;
}
public static void main(String[] args) {
int[] num = {-2,0,1,1,2};
ArrayList<ArrayList<Integer>> lists = new ArrayList<>();
lists = new Three_sum().threeSum(num);
for(ArrayList<Integer> arrayi : lists) {
System.out.print("[");
for(Integer i : arrayi) {
System.out.print(i + " ");
}
System.out.println("]");
}
}
}