167. Two Sum II - Input array is sorted两数之和

1. 原始题目

给定一个已按照升序排列 的有序数组,找到两个数使得它们相加之和等于目标数。

函数应该返回这两个下标值 index1 和 index2,其中 index1 必须小于 index2

说明:

  • 返回的下标值(index1 和 index2)不是从零开始的。
  • 你可以假设每个输入只对应唯一的答案,而且你不可以重复使用相同的元素。

示例:

输入: numbers = [2, 7, 11, 15], target = 9
输出: [1,2]
解释: 2 与 7 之和等于目标数 9 。因此 index1 = 1, index2 = 2 。

2. 思路

双指针法。左右指针分别往中间移,如果两数之和等于target则返回索引,否则如果两数之和大于target,则右端应该往左移来减小两数之和。否则左端指针往右移来增大1两数之和。

3. 解题

 1 class Solution:
 2     def twoSum(self, numbers: List[int], target: int) -> List[int]:
 3         i,j=0,len(numbers)-1
 4         while(i<j):
 5             if numbers[i]+numbers[j]==target:
 6                 return[i+1,j+1]
 7             elif numbers[i]+numbers[j]>target:
 8                 j-=1
 9             else:
10                 i+=1     


原文地址:https://www.cnblogs.com/king-lps/p/10799366.html

时间: 2024-10-14 04:34:40

167. Two Sum II - Input array is sorted两数之和的相关文章

167 Two Sum II - Input array is sorted 两数之和 II - 输入有序数组

给定一个已按照升序排列 的有序数组,找到两个数使得它们相加之和等于目标数.函数应该返回这两个下标值 index1 和 index2,其中 index1 必须小于 index2.请注意,返回的下标值(index1 和 index2)不是从零开始的.你可以假设每个输入都只有一个解决方案,而且你不会重复使用相同的元素.输入:数组 = {2, 7, 11, 15}, 目标数 = 9输出:index1 = 1, index2 = 2详见:https://leetcode.com/problems/two-

[LeetCode] Two Sum II - Input array is sorted 两数之和之二 - 输入数组有序

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 mu

【LeetCode】167. Two Sum II - Input array is sorted

Two Sum II - Input array is sorted 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 a

LeetCode 167. Two Sum II - Input array is sorted (两数之和之二 - 输入的是有序数组)

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 m

167. Two Sum II - Input array is sorted - Easy

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 m

[LC] 167. Two Sum II - Input array is sorted

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 m

Java [Leetcode 167]Two Sum II - Input array is sorted

题目描述: 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 in

167. Two Sum II - Input array is sorted (二分ortwo-pointer)

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 m

LeetCode - 167. Two Sum II - Input array is sorted - O(n) - ( C++ ) - 解题报告

1.题目大意 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 i