leetcode 15 3sum

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;
    }
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章