LeetCode167. 两数之和 II - 输入有序数组(java)

题目:

给定一个已按照升序排列 的有序数组,找到两个数使得它们相加之和等于目标数。

函数应该返回这两个下标值 index1 和 index2,其中 index1 必须小于 index2。

说明:

返回的下标值(index1 和 index2)不是从零开始的。
你可以假设每个输入只对应唯一的答案,而且你不可以重复使用相同的元素。

示例:
LeetCode167. 两数之和 II - 输入有序数组(java)_第1张图片
代码:

  • 解法一
class Solution {
    public int[] twoSum(int[] numbers, int target) {
        if(numbers==null){
            return null;
        }
        int[] indexs=new int[2];
        //先找出一个元素作为加数之一 在求出另一个加数 判断另一个加数是否在数组中(利用二分查找)
        for(int i=0;inumbers[mid]){
                    head=mid+1;
                }
                if(keyrear){
                    flag=false;
                    break;
                }
            }
            if(flag){
                if(mid!=i){
                    indexs[1]=mid+1;
                    return indexs;
                }
            }
        }
        return null;
    }
}
  • 别人的代码
class Solution {
    public int[] twoSum(int[] numbers, int target) {
   //利用夹逼思想 求出结果
        int low = 0, high = numbers.length - 1;
        while(low < high) {
            if(numbers[low] + numbers[high] == target) {
                return new int[] {low+1, high+1};
            } else if(numbers[low] + numbers[high] < target) {
                low++;
            } else {
                high--;
            }
        }
        return null;
    }
}
  • 别人的代码
class Solution {
    public int[] twoSum(int[] numbers, int target) {
        if (numbers == null || numbers.length == 0) {
            return new int[2];
        }
        int start = 0;
        int end = numbers.length - 1;
        while (start < end) {
            if (numbers[start] + numbers[end] == target) {
                return new int[]{start + 1, end + 1};
            } else if (numbers[start] + numbers[end] > target) {
                // move end forward to the last value that numbers[end] <= target - numbers[start]
                end = largestSmallerOrLastEqual(numbers, start, end, target - numbers[start]);
            } else {
                // move start backword to the first value that numbers[start] >= target - numbers[end]
                start = smallestLargerOrFirstEqual(numbers, start, end, target - numbers[end]);
            }
        }
        return new int[2];
    }
    
    private int largestSmallerOrLastEqual(int[] numbers, int start, int end, int target) {
        int left = start;
        int right = end;
        while (left <= right) {
            int mid = left + (right - left) / 2;
            if (numbers[mid] > target) {
                right = mid - 1;
            } else {
                left = mid + 1;
            }
        }
        return right;
    }
    
    private int smallestLargerOrFirstEqual(int[] numbers, int start, int end, int target) {
        int left = start;
        int right = end;
        while (left <= right) {
            int mid = left + (right - left) / 2;
            if (numbers[mid] < target) {
                left = mid + 1;
            } else {
                right = mid - 1;
            }
        }
        return left;
    }
}

你可能感兴趣的:(LeetCode刷题)