LeetCode 167. Two Sum II – Input array is sorted

Two Sum II – Input array is sorted


public class Solution {
    public int[] TwoSum(int[] numbers, int target) {
        int left = 0;
        int right = numbers.Length - 1;
        while(left < right){
            int val = numbers[left] + numbers[right];
            if(val == target){
                return new int[] {left+1, right+1};
            } else if (val > target) right -= 1;
            else left += 1;
        }
        return null;
    }
}

Leave a Reply

Your email address will not be published. Required fields are marked *