3 Sum Closest

now we need find the triplet in a given array.
we only need to find the closest triplets and return it, that’s all.

first, we know there is a limitness of being close, 0, so if we find something which sums to exactly the target, and then we break.
but if we can’t find something like this, we might need to maintain a number, which shows the closest so far, if we can get a more closer one, then we will update this number.

and after some consideration, my code is presented as follows:

3 <= nums.length <= 10^3
-10^3 <= nums[i] <= 10^3
-10^4 <= target <= 10^4

class Solution {
    public int threeSumClosest(int[] nums, int target) {
        int res = Integer.MAX_VALUE;
        
        int start = 0;
        Arrays.sort(nums);
        while (start <= nums.length - 3) {
            int l = start + 1;
            int r = nums.length - 1;
            
            while (l < r) {
                if (nums[start] + nums[l] + nums[r] - target == 0) {
                    return nums[start] + nums[l] + nums[r];
                }
                if (Math.abs(nums[start] + nums[l] + nums[r] - target) < Math.abs(res - target)) {
                    System.out.println("xx");
                    res = nums[start] + nums[l] + nums[r];
                } 
                
                if (nums[start] + nums[l] + nums[r] > target) {
                    r--;
                } else if (nums[start] + nums[l] + nums[r] < target) {
                    l++;
                }
            }
            
            start++;
        }
        return res;
    }
}

and it can be accepted: based on the output, it seems overflow happens.
but how? every elements in that array is within the range of 1000.

the problems lies in" if (Math.abs(nums[start] + nums[l] + nums[r] - target) < Math.abs(res - target)) { ", because res starts with Integer.MAX_VALUE, and if the target is a negative number, then overflow will happens here.
ao i will change res into long when initialized it, and change it back to int.

class Solution {
    public int threeSumClosest(int[] nums, int target) {
        long res = Integer.MAX_VALUE;
        
        int start = 0;
        Arrays.sort(nums);
        while (start <= nums.length - 3) {
            int l = start + 1;
            int r = nums.length - 1;
            
            while (l < r) {
                if (nums[start] + nums[l] + nums[r] - target == 0) {
                    return nums[start] + nums[l] + nums[r];
                }
                if (Math.abs(nums[start] + nums[l] + nums[r] - target) < Math.abs(res - target)) {
                    System.out.println("xx");
                    res = nums[start] + nums[l] + nums[r];
                } 
                
                if (nums[start] + nums[l] + nums[r] > target) {
                    r--;
                } else if (nums[start] + nums[l] + nums[r] < target) {
                    l++;
                }
            }
            
            start++;
        }
        return (int)res;
    }
}
posted @ 2020-09-08 11:56  EvanMeetTheWorld  阅读(14)  评论(0)    收藏  举报