Leetcode 167 Sum of Two Numbers - Enter an Ordered Array

Leetcode 167 Sum of Two Numbers - Enter an Ordered Array

Title description:

You are given an integer array numbers whose subscripts start from 1. The array has been arranged in non-decreasing order. Please find two numbers from the array that satisfy the sum of the sum to be equal to the target number target. If these two numbers are numbers[index1] and numbers[index2] respectively, then 1 <= index1 < index2 <= numbers.length.

Returns the indices index1 and index2 of these two integers as an array of integers of length 2 [index1, index2].

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

The solution you design must use only a constant amount of extra space.

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. So index1 = 1, index2 = 2. returns [1, 2].

Example 2:

Input: numbers = [2,3,4], target = 6
Output: [1,3]
Explanation: The sum of 2 and 4 is equal to the target number 6. So index1 = 1, index2 = 3. returns [1, 3].

Example 3

Input: numbers = [-1,0], target = -1
Output: [1,2]
Explanation: The sum of -1 and 0 is equal to the target number -1. So index1 = 1, index2 = 2. returns [1, 2].

code:

Solution 1:

#双指针法

class Solution:

  def twoSum(self, numbers: List[int], target: int) -> List[int]:

​    low,high=0,len(numbers)-1
while low<high:
sum=numbers[low]+numbers[high]
if sum==target:
return [low+1,high+1]
elif sum<target:

​         low+=1
else:

​         high-=1
	#找不到满足两数之和等于target的两个数return [-1,-1]

This problem can use a typical double-pointer algorithm. Initially, set two pointers low and high to point to the first element position and the last element position respectively. First calculate the maximum possible sum of the sum of the two, and then sum Compare with target, if sum==target, the only solution is found, if sum<target, then low++, if sum>target, then high++, repeat the above operations until the answer is found.

Initially, the two pointers point to subscript 0 and subscript numbers.length−1 respectively, the subscript pointed to by the left pointer is less than or equal to i, and the subscript pointed to by the right pointer is greater than or equal to j. Unless the left and right pointers are already at the subscripts i and j at the beginning, the left pointer must first reach the position of the subscript i or the right pointer must first reach the position of the subscript j.

Solution 2:

#二分法
class Solution:

  def twoSum(self, numbers: List[int], target: int) -> List[int]:

   n=len(numbers)

   for i in range(n):
	#相当于在固定了index1的时候,在[index1+1,n-1]内找index2
	#符合题意,不可以重复使用相同的元素
​     low,high=i+1,n-1
while low<=high:

​       mid=(low+high)//2
if numbers[mid]==target-numbers[i]:
return [i+1,mid+1]
elif numbers[mid]>target-numbers[i]:

​        high=mid-1
else:

​        low=mid+1

  #找不到满足target的两个数

   return [-1,-1]

For this solution, we must first understand that the subscript value in the array is calculated from 0, so when returning the subscript of the corresponding value, we need to perform +1 operation on it. And, to find two numbers in the array so that their sum is equal to the target value, you can first fix the first number, and then find the second number, which is equal to the difference between the target value and the first number , using the ordered nature of the array, we can use the idea of ​​binary search to find the second number. Here, in order to avoid repeated searches, when looking for the second value, we only search on the right side of the first number.

Supongo que te gusta

Origin blog.csdn.net/Duba_zhou/article/details/123789081
Recomendado
Clasificación