167. Two Sum II - Input array is sorted

class Solution {
    public int[] twoSum(int[] numbers, int target) {
        // two pointers
        int start = 0, end = numbers.length-1;
        while(start<end){
            int sum = numbers[start]+numbers[end];
            if(sum==target){
                return new int[]{start+1, end+1};
            }else if(sum>target){
                end--;
            }else{
                start++;
            }
        }
        return null;
    }
}

Last updated