[LeetCode]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) {
            if (numbers[left] + numbers[right] > target) {
                right --;
            } else if (numbers[left] + numbers[right] < target) {
                left ++;
            } else {
                return new int[]{left + 1, right + 1};
            }
        }
        return new int[]{0, 0};
    }
}

 

posted @ 2015-12-04 08:03  Weizheng_Love_Coding  阅读(107)  评论(0编辑  收藏  举报