首页 > 代码库 > 167. Two Sum II - Input array is sorted

167. Two Sum II - Input array is sorted

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

package com.company;

public class Solution {
    public int[] twoSum(int[] numbers, int target) {
        int len=numbers.length;
        if(len==0||len==1)
            return null;
        int[] res=new int[2];
        int right=len-1;
        int left=0;
        while(left<right)
        {
            if(numbers[left]+numbers[right]==target)
            {
                res[0]=left+1;
                res[1]=right+1;
                break;
            }
            else  if(numbers[left]+numbers[right]>target)
            {
                right--;
            }
            else
            {
                left++;
            }
        }
        return res;
    }
}

 

167. Two Sum II - Input array is sorted