两数之和
题目描述:给定一个已按照 升序排列 的整数数组 numbers ,请你从数组中找出两个数满足相加之和等于目标数 target 。
class Solution {
public int[] twoSum(int[] numbers, int target) {
if(numbers==null) return null;
int n=numbers.length;
int i=0,j=n-1;
for(i=0,j=n-1;i<j;){
if(numbers[i]+numbers[j]<target){
i++;
}else if(numbers[i]+numbers[j]>target){
j--;
}else{
return new int[]{i+1,j+1};
}
}
return null;
}
}