Problem : Given an array of integers, if you can find two numbers which make it fit to a specified value?
Thinking : sorted, moved to the middle from both ends of the array, one end of the movement of the pointer, until the two numbers together as the specified value.
java achieve :
boolean hasSum(int[] A,int target) {
boolean res = false;
if (A == null || A.length < 2)
return res;
Arrays.sort(A);
int i = 0, j = A.length - 1;
while (i < j) {
if (A[i] + A[j] == target) {
res = true;
break;
} else if (A[i] + A[j] > target) {
j--;
} else {
i++;
}
}