1. 程式人生 > >LeetCode-最接近的三數之和

LeetCode-最接近的三數之和

給定一個包括 n 個整數的陣列 nums和 一個目標值 target。找出 nums中的三個整數,使得它們的和與 target 最接近。返回這三個數的和。假定每組輸入只存在唯一答案。

例如,給定陣列 nums = [-1,2,1,-4], 和 target = 1.

與 target 最接近的三個數的和為 2. (-1 + 2 + 1 = 2).

package threeSumClosest;

import java.util.Arrays;

public class ThreeSumClosest {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Solution solution = new Solution();
		System.out.println(solution.threeSumClosest(new int[] { 0, 2, 1, -3 }, 1));
	}

}

class Solution {
	public int threeSumClosest(int[] nums, int target) {

		Arrays.sort(nums);
		int result = nums[1] + nums[2] + nums[0];

		int diff = Math.abs(target - result);

		int temp;
		int tempDiff;
	

		//System.out.println(result);
		for (int i = 0; i < nums.length - 2; ++i) 
		{
			int left = i+1;
			int right = nums.length - 1;
			
			while (left < right) {
				temp = nums[i] + nums[left] + nums[right];

				tempDiff = target - temp;
				if (tempDiff > 0) {
					++left;
				} else {
					--right;
				}
				if (diff > Math.abs(tempDiff)) {
					diff = Math.abs(tempDiff);
					result = temp;

				}

				if (diff == 0) {
					break;
				}

				// System.out.println(result);
			}
		}
		return result;

	}
}