天天看點

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

Two Sum II - Input array is sorted思路:弱智題目

GitHub位址:https://github.com/corpsepiges/leetcode

點此進入如果可以的話,請點一下star,謝謝。

public class Solution {
    public int[] twoSum(int[] numbers, int target) {
        int[] ans=new int[2];
        int s=0;
        int e=numbers.length-1;
        while (s<e) {
            if (numbers[s]+numbers[e]==target) {
                ans[0]=s+1;
                ans[1]=e+1;
                return ans;
            }else if(numbers[s]+numbers[e]<target) {
                s++;
            }else{
                e--;
            }
        }
        return null;
    }
}