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.
my answer:
import java.util.*;
public class Solution {
public static List<List<Integer>> threeSum(int[] nums) {
List<Integer> list_in = new ArrayList<Integer>();
List<List<Integer>> list_out = new ArrayList<List<Integer>>();
if(nums == null || nums.length<3)
return list_out;
Arrays.sort(nums);
<span style="white-space:pre"> </span>for(int j=i+1;j<nums.length;j++){
if(nums[i]+nums[j]>0 && nums[j]>0) break;
for(int k=j+1;k<nums.length;k++){
if(nums[i]+nums[j]+nums[k]==0){
list_in.add(nums[i]);
list_in.add(nums[j]);
list_in.add(nums[k]);
if(!list_out.contains(list_in))
list_out.add(list_in);
list_in=new ArrayList<Integer>();
}
}
}
return list_out;
}
public static void main(String[] args){
List<List<Integer>> list =threeSum(new int[]{-1,0,1,2,-1,-4});
//List<List<Integer>> list =threeSum(new int[]{0,0,0,0,0});
//List<List<Integer>> list =threeSum(new int[]{0,0,0,0,0});
//List<List<Integer>> list =threeSum(new int[]{-4,-2,-2,-2,0,1,2,2,2,3,3,4,4,6,6});
//List<List<Integer>> list =threeSum(new int[]{-15,6,7,0,-14,-5,-3,-10,-14,1,-14,-1,-11,-11,-15,-1,3,-12,7,14,1,6,-6,7,1,1,0,-4,8,7,2,1,-2,-6,-14,-9,-3,-1,-12,-2,7,11,4,12,-14,-4,-4,4,-1,10,3,-14,1,12,0,10,-9,8,-9,14,-8,8,0,-3,10,-6,4,-8,0,-1,-3,-8,-4,8,11,-3,-11,-8,8,3,10,-3,-4,-4,-14,12,13,-8,-3,12,-8,4,5,-1,-14,-8,8,-3,-9,-15,12,-5,-7,-15,-12,11,-11,14,11,12,3,6,-6});
System.out.println(list);
}
}
唔,正如我所料,刚开始还是对的,后来数据一多果然超时了,毕竟3个forO(n^3)呐!!!
test到最后[7,-1,14,-12,-8,7,2,-15,8,8,-8,-14,-4,-5,7,9,11,-4,-15,-6,1,-14,4,3,10,-5,2,1,6,11,2,-2,-5,-7,-6,2,-15,11,-6,8,-4,2,1,-1,4,-6,-15,1,5,-15,10,14,9,-8,-6,4,-6,11,12,-15,7,-1,-9,9,-1,0,-4,-1,-12,-2,14,-9,7,0,-3,-4,1,-2,12,14,-10,0,5,14,-1,14,3,8,10,-8,8,-5,-2,6,-11,12,13,-7,-12,8,6,-13,14,-2,-5,-11,1,3,-6]
果不其然的挂掉了...嘤嘤嘤
参考:http://www.programcreek.com/2012/12/leetcode-3sum/
改进:
import java.util.*;
public class Solution_15 {
public static List<List<Integer>> threeSum(int[] nums) {
List<Integer> list_in = new ArrayList<Integer>();
List<List<Integer>> list_out = new ArrayList<List<Integer>>();
if(nums == null || nums.length<3)
return list_out;
Arrays.sort(nums);
for (int i=0;i<nums.length-2;i++){
if(nums[0]>0)
break;
if(i>1 && nums[i] == nums[i-1])
continue;
int l = i+1;
int r = nums.length-1;
int sum = 0;
while(l<r){
sum = nums[i]+nums[l]+nums[r];
if (sum==0){
list_in.add(nums[i]);
list_in.add(nums[l]);
list_in.add(nums[r]);
if(!list_out.contains(list_in))
list_out.add(list_in);
list_in=new ArrayList<Integer>();
l++;r--;
} else if(sum>0)
r--;
else if(sum<0)
l++;
}
}
return list_out;
}
public static void main(String[] args){
List<List<Integer>> list =threeSum(new int[]{-1,0,1,2,-1,-4});
//List<List<Integer>> list =threeSum(new int[]{0,0,0,0,0});
//List<List<Integer>> list =threeSum(new int[]{0,0,0,0,0});
//List<List<Integer>> list =threeSum(new int[]{-4,-2,-2,-2,0,1,2,2,2,3,3,4,4,6,6});
//List<List<Integer>> list =threeSum(new int[]{-15,6,7,0,-14,-5,-3,-10,-14,1,-14,-1,-11,-11,-15,-1,3,-12,7,14,1,6,-6,7,1,1,0,-4,8,7,2,1,-2,-6,-14,-9,-3,-1,-12,-2,7,11,4,12,-14,-4,-4,4,-1,10,3,-14,1,12,0,10,-9,8,-9,14,-8,8,0,-3,10,-6,4,-8,0,-1,-3,-8,-4,8,11,-3,-11,-8,8,3,10,-3,-4,-4,-14,12,13,-8,-3,12,-8,4,5,-1,-14,-8,8,-3,-9,-15,12,-5,-7,-15,-12,11,-11,14,11,12,3,6,-6});
System.out.println(list);
}
}
Runtime:33ms
然后然后,恩,1.11 星期一,击败了11.11% 嗷嗷~~