Fast and easy-understanding Java solution


  • 1
    Y
    public int threeSumClosest(int[] nums, int target) {
        Arrays.sort(nums);
        int diff = Integer.MAX_VALUE;
        int output = 0;
        for(int i = 0; i < nums.length - 2; i++){
            int low = i + 1;
            int high = nums.length - 1;
            while(low < high){
                int sum = nums[i] + nums[low] + nums[high];
                if(Math.abs(target - sum) < diff){
                   diff = Math.abs(target - sum);
                   output = sum;
                } 
                if(sum > target) high--;
                else low++;
            }
        }
        return output;
    }

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.