美文网首页
167. 两数之和 II - 输入有序数组

167. 两数之和 II - 输入有序数组

作者: Andysys | 来源:发表于2019-12-30 22:25 被阅读0次
    public int[] twoSum(int[] numbers, int target) {
        int[] res = new int[2];
        int left = 0, right = numbers.length - 1;
        while (left < right) {
            if (numbers[left] + numbers[right] > target) {
                right--;
            } else if (numbers[left] + numbers[right] < target) {
                left++;
            } else {
                res[0] = left + 1;
                res[1] = right + 1;
                return res;
            }
        }
        return res;
    }

相关文章

网友评论

      本文标题:167. 两数之和 II - 输入有序数组

      本文链接:https://www.haomeiwen.com/subject/fnbeoctx.html