LeetCode brush question: Double pointer [167. The sum of two numbers II - input ordered array] - Java version

Double pointer: sum of two numbers

I just record my own process of brushing the questions, and I have referred to various solutions for the answers.

Thank you for correcting me if I make any mistakes!

Reference: LeetCode 101: A LeetCode Grinding Guide (C++ Version) Author: Gao Chang Chang Gao

Source of questions: Question Bank - LeetCode, the global geek's favorite technology growth platform (leetcode-cn.com)

A. Double pointer thinking

  • Because the array is already sorted, we can use double pointers in opposite directions to find the two numbers.

B. Concrete implementation

  • Two pointers, one initially points to the smallest element, that is, the leftmost element of the array, and traverses to the right; the other initially points to the largest element, that is, the rightmost element of the array, and traverses to the left.

  • If the sum of the elements pointed to by the two pointers is equal the given value, then they are the result we want; if the sum of the elements pointed to by the two pointers is less than given value, we move the left pointer to the right by one bit, so that the current sum increases a little ; If the sum of the elements pointed to by the two pointers is greater than the given value, we move the right pointer to the left by one bit, so that the current sum is reduced by a bit.

c. to think

  • It can be proved that for an array that is sorted and has a solution, the double pointer must be able to traverse to the optimal solution.

  • The proof method is as follows: Assume that the positions of the two numbers of the optimal solution are l and r respectively. We assume that when the left pointer is on the left of l, the right pointer has moved to r; at this time, the sum of the values ​​pointed by the two pointers is less than the given value, so the left pointer will move right until it reaches l. Similarly, if we assume that when the right pointer is on the right of r, the left pointer has moved to l; at this time, the sum of the values ​​pointed by the two pointers is greater than the given value, so the right pointer will always move left until it reaches r. Therefore, it is impossible for the double pointer to be between (l, r) at any time, and because the pointer must move one when the condition is not met, it will eventually converge on l and r.

  • If the array is not sorted, and the title requirements have nothing to do with the array order, you can consider adopting the method of sorting first and then double pointers.

  • Think in terms of reducing the search space . Regardless A[i] + A[j]of the result of is large or small, we can exclude a row or a column of the search space. After n steps, all search spaces can be excluded and all possibilities can be checked.

D. code

public int[] twoSum(int[] numbers, int target) {
    int start = 0, end = numbers.length - 1, sum;
    while (start < end) {
    sum = numbers[start] + numbers[end];
    	if (sum == target) {
    		return new int[]{start + 1, end + 1}; //按要求返回新数组
        } else if (sum < target) { // 结果小于给定值,则开始的指针下标自增
            ++start;
        } else { // 结果大于给定值,则结束的指针下标自减
            --end;
        }
    }
    return new int[]{-1, -1};
}

Guess you like

Origin blog.csdn.net/weixin_51909882/article/details/123221618