Problem: 167. 两数之和 II - 输入有序数组
复杂度
时间复杂度: O ( n ) O(n) O(n)
空间复杂度: O ( 1 ) O(1) O(1)
Code
class Solution {
public int[] twoSum(int[] numbers, int target) {
int l = 0;
int r = numbers.length-1;
while(l < r)
{
if(numbers[l] + numbers[r] > target)
r--;
else if(numbers[l] + numbers[r] < target)
l++;
else
return new int[]{l+1,r+1};
}
return null;
}
}