2017-06-10 19:31:17 +0000   |     algorithm leetcode binary search two pointers   |   Viewed times   |    

题目

Given an array of integers that is already sorted in ascending order, find two numbers such that they add up to a specific target number.

The function twoSum should return indices of the two numbers such that they add up to the target, where index1 must be less than index2. Please note that your returned answers (both index1 and index2) are not zero-based.

You may assume that each input would have exactly one solution and you may not use the same element twice.

Input: numbers={2, 7, 11, 15}, target=9 Output: index1=1, index2=2

老实遍历,时间复杂度

两个指针slowfast两层套嵌遍历。

代码

public class Solution {
    public int[] twoSum(int[] numbers, int target) {
        int[] res = new int[]{-1,-1};
        if (numbers.length < 2) { return res; }
        int slow = 0;
        while (slow < numbers.length-1 && numbers[slow] <= target) {
            int num1 = numbers[slow];
            for (int fast = slow + 1; fast < numbers.length; fast++) {
                int sum = num1 + numbers[fast];
                if (sum == target) { // find target
                    res[0] = slow+1; res[1] = fast+1;
                    return res;
                } else if (sum > target) {
                    break;
                }
            }
            slow++;
        }
        return res; // do not find target
    }
}

结果

two-sum-two-1

slow指针老实遍历,fast指针二分查找,复杂度

因为数组预先排过序,比如2,7,11,15,19,21target = 18,当slow指向2fast就可以做一个7,11,15,19,21的二分查找remain = 18-2 = 16

 slow fast
    | |
    2,7,11,15,19,21
      |--二分查找--|

代码

public class Solution {
    public int[] twoSum(int[] numbers, int target) {
        int[] res = new int[] {-1,-1};
        if (numbers.length < 2) { return res; }
        int slow = 0;
        while (slow < numbers.length-1 && numbers[slow] <= target) {
            int remain = target - numbers[slow];
            int lo = slow+1, hi = numbers.length-1;
            while (lo <= hi) {
                int mid = lo + (hi - lo) / 2;
                int median = numbers[mid];
                if (median < remain) {
                    lo = mid+1;
                } else if (median > remain) {
                    hi = mid-1;
                } else { // median == remain
                    res[0] = slow+1; res[1] = mid+1;
                    return res; // find target
                }
            }
            slow++;
        }
        return res; // do not find target
    }
}

结果

two-sum-two-2

Two Pointers的标准解法,复杂度

Two Sum一样,用两个指针lohi分别指向数组边界,

target = 18

    lo           hi
    |            |
    2,7,11,15,19,21         //  2 + 21 > 18   ->  hi--

    lo         hi
    |          |
    2,7,11,15,19,21         //  2 + 19 > 18   ->  hi--

    lo      hi
    |       |
    2,7,11,15,19,21         //  2 + 15 < 18   ->  lo++

      lo    hi
      |     |
    2,7,11,15,19,21         //  7 + 15 > 18   ->  hi--

      lo hi
      |  |
    2,7,11,15,19,21         //  7 + 11 == 18   ->  return

代码

public class Solution {
    public int[] twoSum(int[] numbers, int target) {
        int[] res = new int[] {-1,-1};
        if (numbers.length < 2) { return res; }
        int lo = 0, hi = numbers.length-1;
        while (lo < hi) {
            int sum = numbers[lo] + numbers[hi];
            if (sum < target) {
                lo++;
            } else if (sum > target) {
                hi--;
            } else { // sum == target [FIND!!!]
                res[0] = lo+1; res[1] = hi+1;
                break;
            }
        }
        return res;
    }
}

结果

two-sum-two-3