Leetcode刷题—1:有序数组的 Two Sum

使用双指针
class Solution {
public int[] twoSum(int[] numbers, int target) {
int i=0,j=numbers.length-1;
while (i<j){
if (numbers[i]+numbers[j]==target){
return new int[]{i+1,j+1};
}
else if (numbers[i]+numbers[j]>target){
j–;
}
else {
i++;
}
}
return null;
}
}


版权声明:本文为weixin_42663941原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。