LeetCode——三数之和

public List<List<Integer>> threeSum(int[] nums) {
       
        List<List<Integer>> result = new ArrayList<>();
        if(nums.length < 3)return result;
        Arrays.sort(nums);
        
        for(int i =  0 ; i < nums.length -2 ; i++)
        {
            if(nums[i] > 0)
                break;
            if(i == 0 || nums[i] != nums[i-1]) //去重
            {
                int start = i + 1;
                int end = nums.length - 1;
                while(start < end)
                {
                    int cur = nums[i] + nums[start] + nums[end];
                    if(cur == 0 )
                    {
                        List<Integer> list = new ArrayList<>();
                        list.add(nums[i]);
                        list.add(nums[start]);
                        list.add(nums[end]);
                        result.add(list);
                        start++;
                        end--;
                        while(start < end && nums[start] == nums[start -1]) //去重
                        {
                            start++;
                        }
                        while(start < end && nums[end] == nums[end+1]) //去重
                        {
                            end--;
                        }

                    }
                    else if(cur < 0)
                    {
                        start++;
                    }
                    else
                    {
                        end--;
                    }
                }
            }
        }
        return result;
    }
原文地址:https://www.cnblogs.com/swqblog/p/13236590.html