[LeetCode16]3Sum Closest

Given an array S of n integers, find three integers in S such that the sum is closest to a given number, target. Return the sum of the three integers. You may assume that each input would have exactly one solution.

    For example, given array S = {-1 2 1 -4}, and target = 1.

    The sum that is closest to the target is 2. (-1 + 2 + 1 = 2).
Analysis:

Same with 3Sum. Just need a variable to track the minimum number and result.

Java

public int threeSumClosest(int[] num, int target) {
       int len = num.length;
		Arrays.sort(num);
		int min = Integer.MAX_VALUE;
		int record = 0;
		for(int i=0;i<len;i++){
			int start = i+1;
			int end = len-1;
			while(start<end){
				int sum = num[i]+num[start]+num[end];
				if(sum == target){
//					min = 0;
					record = sum;
					return record;
				}
				else if (sum>target) {
					if(sum-target<min){
						min = sum-target;
						record = sum;
					}
					end--;
				}else {
					if(target-sum<min){
						min = target-sum;
						record = sum;
					}
					start++;
				}
			}
			while(i<len-1 && num[i]==num[i+1]) i++;
		}
		return record; 
    }

public int threeSumClosest(int[] nums, int target) {
        int result = 0;
        if(nums.length<3) return result;
        int gm = Integer.MAX_VALUE;
        Arrays.sort(nums);
        for(int i=0;i<nums.length-2;i++) {
            if(i>0 && nums[i] == nums[i-1]) continue;
            int start = i+1;
            int end = nums.length-1;
            while(start<end) {
                int sum = nums[i]+nums[start]+nums[end];
                if(Math.abs(sum-target) <gm) {
                    gm = Math.abs(sum-target);
                    result = sum;
                }
                if(target == sum){//assume only has one result
                    return sum;
                }else if(sum>target) {
                    end--;
                    while(end>start && nums[end] == nums[end+1]) end--;
                }else{
                    start++;
                    while(end>start && nums[start] == nums[start-1]) start++;
                }
            }
            while(i<nums.length-2 && nums[i]==nums[i+1]) i++;
        }
        return result;
    }



c++

int threeSumClosest(vector<int> &num, int target) {
    int result;
    vector<int> solution;
    sort(num.begin(), num.end());
    int minD = INT_MAX;
    for(int i=0; i<num.size()-2;i++){
        int start = i+1;
        int end = num.size()-1;
        while(start < end){
            int sum = num[i]+num[start]+num[end];
            if(sum == target){
                minD = 0;
                result = sum;
                return result;
            }
            else if(sum<target){
                if(target-sum < minD){
                    minD = target-sum;
                    result = sum;
                }
                start++;
            }
            else{
                if(sum-target < minD){
                    minD = sum-target;
                    result = sum;
                }
                end--;
            }
        }
        while(i<num.size() && num[i] == num[i+1]) i++;
    }
    return result;
    }






你可能感兴趣的:(LeetCode)