16.LeetCode 最接近的三个数之和

与上一道题及其相似 也没什么好办法

  public int threeSumClosest1(int[] nums, int target) {
        Arrays.sort(nums);
        int len = nums.length;
        int sum = 0;
        int max = Integer.MAX_VALUE;

        for (int first = 0; first < len; first++) {
            int second = first + 1, third = nums.length - 1;
            while (third>second){
                if (nums[first]+nums[second]+nums[third]==target){
                    return target;
                }
                
                if (Math.abs((nums[first]+nums[second]+nums[third]-target))<max){
                    sum = nums[first]+nums[second]+nums[third];
                    max =Math.abs(sum-target);
                }
                if(nums[first]+nums[second]+nums[third]>target){
                   third --;
                }else{
                    second++;
                }
            }
        }

        return sum;

    }

 

上一篇:Spring Boot系列之多数据源配置


下一篇:boost::mp11::mp_replace_third相关用法的测试程序