Leikou Brushing Notes: 167. Sum of Two Numbers II-Input an ordered array (Dichotomy + double pointer method, double pointer method is faster, and the code is easy to understand)

topic:

167. Sum of Two Numbers II-Input Ordered Array

Given an integer array numbers arranged in ascending order, please find two numbers from the array that satisfy the sum of the target number target.

The function should return the subscript values ​​of these two numbers in the form of an integer array of length 2. The subscript of numbers starts counting from 1, so the answer array should satisfy 1 <= answer[0] <answer[1] <= numbers.length.

You can assume that each input only corresponds to a unique answer, and you cannot reuse the same elements.

Example 1:

Input: numbers = [2,7,11,15], target = 9
Output: [1,2]
Explanation: The sum of 2 and 7 is equal to the target number 9. Therefore index1 = 1, index2 = 2.

Example 2:

Input: numbers = [2,3,4], target = 6
Output: [1,3]

Example 3:

Input: numbers = [-1,0], target = -1
Output: [1,2]

prompt:

2 <= numbers.length <= 3 * 10^4
-1000 <= numbers[i] <= 1000
numbers are arranged in increasing order
-1000 <= target <= 1000 There
is only one valid answer

Problem solution ideas:

1. The idea of ​​binary search method:

Find two numbers in the array so that their sum is equal to the target value. You can fix the first number first, and then find the second number. The second number is equal to the target value minus the difference of the first number. Using the ordered nature of the array, the second number can be found by binary search. In order to avoid repeated searching, when searching for the second number, only search on the right side of the first number. The writing method can be written directly according to the binary search template.

Time complexity O(nlog(n))

Insert picture description here
Problem solution python code:

# 二分查找法
class Solution:
    def twoSum(self, numbers: List[int], target: int) -> List[int]:
        n = len(numbers)
        for i in range(n-1):
            left, right = i+1, n-1  #标准二分查找模板写法
            while left<=right: 
                mid = (left+right)//2
                if numbers[mid]==target-numbers[i]:
                    return [i+1, mid+1]
                if numbers[mid]>target-numbers[i]:
                    right = mid-1
                else:
                    left = mid+1
        return [-1, -1]

Two, the idea of ​​double pointer method:

For the array given in the title, define two pointers at the beginning and the end. During the entire movement, the left pointer cannot be moved to the right of ii, and the right pointer cannot be moved to the left of jj, so the possible solutions will not be filtered out. .

When the pointer moves, compare the sum of the values ​​pointed by the left and right pointers with the size of the target. If it is equal to, the answer will be returned directly. If the pointer is small, the left pointer will move to the right, and if it is large, the right pointer will move to the left.

Since the question is guaranteed to have a unique answer, the answer can be found using double pointers.

Time complexity O(n)

Insert picture description here
Problem solution python code:

# 双指针法
class Solution:
    def twoSum(self, numbers: List[int], target: int) -> List[int]:
        n = len(numbers)
        left, right = 0, n-1
        while left<=right:
            total = numbers[left]+numbers[right]
            if total==target:
                return [left+1, right+1]
            elif total<target:
                left += 1
            else:
                right -= 1
        return [-1, -1]

Author: a-qing-ge
Links: https://leetcode-cn.com/problems/two-sum-ii-input-array-is-sorted/solution/yi-er-fen-cha-zhao-fa-shi -jian-fu-za-du-po65c/
Source: LeetCode https://leetcode-cn.com/problems/two-sum-ii-input-array-is-sorted/

Guess you like

Origin blog.csdn.net/weixin_44414948/article/details/113826224