Open zhuangjinxin opened 6 years ago
解答:
public List<List<Integer>> threeSum(int[] nums) {
Arrays.sort(nums);
System.out.println(Arrays.toString(nums));
List<List<Integer>> list = new ArrayList<List<Integer>>();
if (nums.length<3){
return list;
}
for(int i=0;i<=nums.length-3;i++){
for(int j=i+1;j<=nums.length-2;j++){
for(int k=j+1;k<=nums.length-1;k++){
if(nums[i]+nums[j]+nums[k]==0){
List<Integer> temp = new ArrayList<Integer>();
temp.add(nums[i]);
temp.add(nums[j]);
temp.add(nums[k]);
list.add(temp);
}
}
}
}
return list;
}
问题:
题目: 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.