板子题做多了的弊病,条件总是考虑不全。再接再厉!
class Solution { public List<List<Integer>> threeSum(int[] nums) { List<List<Integer>> res = new ArrayList<>(); int len = nums.length; Arrays.sort(nums); int left,right; int ans=0; for(int i=0;i<len;i++){ left=i+1; right=len-1; ans = nums[i]; if(i!=0&&nums[i]==nums[i-1]){ continue; } while(left<right){ if(left!=(i+1)&&nums[left]==nums[left-1]){ left++; continue; } if((ans+nums[left]+nums[right])==0){ List<Integer>a = new ArrayList<Integer>(); a.add(ans); a.add(nums[left]); a.add(nums[right]); res.add(a); left++;right--; continue; } if((ans+nums[left]+nums[right])>0){ right--; continue; } if((ans+nums[left]+nums[right])<0){ left++; continue; } } } return res; } }
原文:https://www.cnblogs.com/dloooooo/p/13758938.html