Array Two Pointers Binary Search

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. Note:

  • 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.
相似题目
简单 Two Sum
Two Sum IV - Input is a BST
Two Sum Less Than K

二分法

可以首先固定第一个数,然后寻找第二个数,第二个数等于目标值减去第一个数的差。利用数组的有序性质,可以通过二分查找的方法寻找第二个数。为了避免重复寻找,在寻找第二个数时,只在第一个数的右侧寻找

  1. class Solution {
  2. public int[] twoSum(int[] numbers, int target) {
  3. for (int i = 0; i < numbers.length; i++) {
  4. // 去寻找numbers[i]是否有目标值
  5. int low = i + 1, high = numbers.length - 1;
  6. while (low <= high) {
  7. int mid = low + (high - low) / 2;
  8. if (numbers[mid] + numbers[i] == target) {
  9. return new int[] {i + 1, mid + 1};
  10. } else if (numbers[mid] + numbers[i] > target) {
  11. high = mid - 1;
  12. } else {
  13. low = mid + 1;
  14. }
  15. }
  16. }
  17. return null;
  18. }
  19. }
  • 时间复杂度:O(nlogn),其中 n 是数组的长度。需要遍历数组一次确定第一个数,时间复杂度是 O(n),寻找第二个数使用二分查找,时间复杂度是 O(logn),因此总时间复杂度是 O(nlogn)。
  • 空间复杂度:O(1)


双指针法

初始时两个指针分别指向第一个元素位置和最后一个元素的位置。每次计算两个指针指向的两个元素之和,并和目标值比较。如果两个元素之和等于目标值,则发现了唯一解。如果两个元素之和小于目标值,则将左侧指针右移一位。如果两个元素之和大于目标值,则将右侧指针左移一位。移动指针之后,重复上述操作,直到找到答案。

使用双指针的实质是缩小查找范围。那么会不会把可能的解过滤掉?答案是不会。假设167. Two Sum II - Input array is sorted - 图1是唯一解,其中167. Two Sum II - Input array is sorted - 图2。初始时两个指针分别指向下标 0 和下标167. Two Sum II - Input array is sorted - 图3,左指针指向的下标小于或等于 i,右指针指向的下标大于或等于 j。除非初始时左指针和右指针已经位于下标 i 和 j,否则一定是左指针先到达下标 i 的位置或者右指针先到达下标 j 的位置。

  • 如果左指针先到达下标 i 的位置,此时右指针还在下标 j 的右侧,167. Two Sum II - Input array is sorted - 图4,因此一定是右指针左移,左指针不可能移到 i 的右侧。
  • 如果右指针先到达下标 j 的位置,此时左指针还在下标 i 的左侧,167. Two Sum II - Input array is sorted - 图5,因此一定是左指针右移,右指针不可能移到 j 的左侧。

由此可见,在整个移动过程中,左指针不可能移到 i 的右侧,右指针不可能移到 j 的左侧,因此不会把可能的解过滤掉。由于题目确保有唯一的答案,因此使用双指针一定可以找到答案。

  1. class Solution {
  2. public int[] twoSum(int[] numbers, int target) {
  3. int low = 0, high = numbers.length - 1;
  4. while (low < high) {
  5. int sum = numbers[low] + numbers[high];
  6. if (sum == target) {
  7. return new int[] {low + 1, high + 1};
  8. } else if (sum < target) {
  9. low++;
  10. } else {
  11. high--;
  12. }
  13. }
  14. return null;
  15. }
  16. }