标签:style blog color os io ar div log sp
problem: given an array of integers including positive and negative, a target value. find 2 numbers in the array such that the sum of the 2 numbers is the closest to the target value.
Solution: this can be solved in O(nlogn).
First, invest some time to sort the array, O(nlogn).
Then,
int min= Integer.MAX; while(i< j){ if(A[i]+ A[j]== T) return (i,j); (A[i]+ A[j]- T> 0)? j--: i++; }
find the closest sum to a target value
标签:style blog color os io ar div log sp
原文地址:http://www.cnblogs.com/miaoz/p/3938578.html