问题:
Given an array of integers, find two numbers such that they add up to a specific target number.
The function twoSum should return indices of the two numbers such that they add up to the target, where index1 must be less than index2. Please note that your returned answers (both index1 and index2) are not zero-based.
You may assume that each input would have exactly one solution.
Input: numbers={2, 7, 11, 15}, target=9
Output: index1=1, index2=2
public class Solution {
class pair{
int num;
int pointer;
}
private pair[] numList;
public int[] twoSum(int[] numbers, int target) {
numList=new pair[numbers.length]
for(int i=0;i<numbers.length;i++)
{
pair p=new pair();
p.num=numbers[i];
p.pointer=i+1;
numList[i]=p;
}
java.util.Arrays.sort(numList, new java.util.Comparator<pair>() {
public int compare(pair o1, pair o2) {
if(o1.num<o2.num)
return -1;
else
if(o1.num>o2.num)
return 1;
else
return 0;
}
});
int[] an=new int[2];
int left=0;
int right=numbers.length-1;
while((left<right)&&(numList[left].num+numList[right].num!=target))
{
if(left>=numbers.length||right<0)
break;
if(numList[left].num+numList[right].num<target)
left++;
if(numList[left].num+numList[right].num>target)
right--;
}
if(numList[left].pointer<numList[right].pointer)
{
an[0]=numList[left].pointer;
an[1]=numList[right].pointer;
}
else
{
an[1]=numList[left].pointer;
an[0]=numList[right].pointer;
}
return an;
}
}