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;
    }
}

 

posted @ 2017-08-06 22:38  阿怪123  阅读(128)  评论(0编辑  收藏  举报