Tuesday, November 17, 2015

[leetcode] Two Sum Sorted Array

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
public class Solution {
    public int[] twoSum(int[] numbers, int target) {
   if (numbers == null || numbers.length <= 1) return null;
   int result[] = new int[2];
   int left = 0;
   int right = numbers.length-1;
   while (left < right){
    if (numbers[left]+numbers[right] == target){
     result[0] = left+1;
     result[1] = right+1;
     return result;
    }else if (numbers[left]+numbers[right] > target){
     right--;
    }else{
     left++;
    }
   }

   return null;
  }
}

No comments:

Post a Comment