public List<List<Integer>> threeSum(int[] nums) {
List<List<Integer>> result= new ArrayList<>();
List<String> diffSum = new ArrayList<>();
if(nums==null || nums.length<3) {
return result;
}
Arrays.sort(nums);
for(int i = 0; i<=nums.length-3;i++){
int low = i+1;
int high = nums.length-1;
while(low<high){
int sum = nums[i]+nums[low]+nums[high];
if(0==sum){
String num = nums[i]+""+nums[low]*10+nums[high];
if(!diffSum.contains(num)){
diffSum.add(num);
List<Integer> temp = new ArrayList<>();
temp.add(nums[i]);
temp.add(nums[low]);
temp.add(nums[high]);
result.add(temp);
}
low++;
}else if(0>sum){
low++;
}else{
high--;
}
}
}
return result;
}
leetcode 15 3sum
最新推荐文章于 2020-06-12 14:31:56 发布