[LeetCode] 167. Two Sum II - Input array is sorted

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[LeetCode] 167. Two Sum II - Input array is sorted相关的知识,希望对你有一定的参考价值。

https://leetcode.com/problems/two-sum-ii-input-array-is-sorted/

类似于 binary search

public class Solution {
    public int[] twoSum(int[] numbers, int target) {
        int[] result = new int[2];
        int start = 0;
        int end = numbers.length - 1;
        while (start < end) {
            if (numbers[start] + numbers[end] == target) {
                result[0] = start + 1;
                result[1] = end + 1;
                break;
            } else if (numbers[start] + numbers[end] > target) {
                end--;
            } else {
                start++;
            }
        }
        return result;
    }
}

 

以上是关于[LeetCode] 167. Two Sum II - Input array is sorted的主要内容,如果未能解决你的问题,请参考以下文章

28.leetcode167_two_sum_II

LeetCode167. Two Sum II - Input array is sorted

LeetCode:167. Two Sum II - Input array is sorted

[LeetCode] 167. Two Sum II - Input array is sorted

LeetCode 167. Two Sum II - Input array is sorted

LeetCode_167. Two Sum II - Input array is sorted