当前位置:网站首页>【Leetcode】167-两数之和II -输入有序数组
【Leetcode】167-两数之和II -输入有序数组
2022-07-02 12:09:00 【酥酥~】
给你一个下标从 1 开始的整数数组 numbers ,该数组已按 非递减顺序排列 ,请你从数组中找出满足相加之和等于目标数 target 的两个数。如果设这两个数分别是 numbers[index1] 和 numbers[index2] ,则 1 <= index1 < index2 <= numbers.length 。
以长度为 2 的整数数组 [index1, index2] 的形式返回这两个整数的下标 index1 和 index2。
你可以假设每个输入 只对应唯一的答案 ,而且你 不可以 重复使用相同的元素。
你所设计的解决方案必须只使用常量级的额外空间。
示例 1:
输入: numbers = [2,7,11,15], target = 9
输出: [1,2]
解释: 2 与 7 之和等于目标数 9 。因此 index1 = 1, index2 = 2 。返回 [1, 2] 。
示例 2:
输入: numbers = [2,3,4], target = 6
输出: [1,3]
解释: 2 与 4 之和等于目标数 6 。因此 index1 = 1, index2 = 3 。返回 [1, 3] 。
示例 3:
输入: numbers = [-1,0], target = -1
输出: [1,2]
解释: -1 与 0 之和等于目标数 -1 。因此 index1 = 1, index2 = 2 。返回 [1, 2] 。
提示:
- 2 <= numbers.length <= 3 * 104
- -1000 <= numbers[i] <= 1000
- numbers 按 非递减顺序 排列
- -1000 <= target <= 1000
- 仅存在一个有效答案
#首先是使用双循环,果不其然超时了
class Solution(object):
def twoSum(self, numbers, target):
length = len(numbers)
first = 0
second = 1
while first<length-1:
while second<length:
if numbers[first]+numbers[second]==target:
return [first+1,second+1]
second+=1
first+=1
second=first+1
#然后将第二层循环替换为二分查找:
class Solution(object):
def twoSum(self, numbers, target):
length = len(numbers)
first = 0
while first<length-1:
left = first+1
right = length-1
while left<=right:
mid = int((left+right)/2)
if numbers[first]+numbers[mid]==target:
return [first+1,mid+1]
elif numbers[first]+numbers[mid]<target:
left = mid+1
else:
right = mid-1
first+=1
return [-1,-1]
#双指针
class Solution(object):
def twoSum(self, numbers, target):
length = len(numbers)
left = 0
right = length-1
while left<right:
if numbers[left]+numbers[right]==target:
return [left+1,right+1]
elif numbers[left]+numbers[right]<target:
left+=1
else:
right-=1
return [-1,-1]
边栏推荐
- Deploy tidb cluster with tiup
- Leetcode skimming - remove duplicate letters 316 medium
- Huffman tree: (1) input each character and its weight (2) construct Huffman tree (3) carry out Huffman coding (4) find hc[i], and get the Huffman coding of each character
- Learn the method code example of converting timestamp to uppercase date using PHP
- The traversal methods of binary tree mainly include: first order traversal, middle order traversal, second order traversal, and hierarchical traversal. First order, middle order, and second order actu
- 党史纪实主题公益数字文创产品正式上线
- 03. Preliminary use of golang
- 做好抗“疫”之路的把关人——基于RK3568的红外热成像体温检测系统
- 12_Redis_Bitmap_命令
- Map introduction
猜你喜欢

语义分割学习笔记(一)

【网络安全】网络资产收集

LeetCode刷题——两整数之和#371#Medium

Leetcode skimming -- incremental ternary subsequence 334 medium

Facing the challenge of "lack of core", how can Feiling provide a stable and strong guarantee for customers' production capacity?

Leetcode skimming -- verifying the preorder serialization of binary tree # 331 # medium

2022 college students in Liaoning Province mathematical modeling a, B, C questions (related papers and model program code online disk download)

How to intercept the value of a key from the JSON string returned by wechat?

彻底弄懂浏览器强缓存和协商缓存

Equipped with Ti am62x processor, Feiling fet6254-c core board is launched!
随机推荐
Leetcode skimming - remove duplicate letters 316 medium
高考录取分数线爬虫
Custom exception
SQL transaction
LeetCode刷题——递增的三元子序列#334#Medium
17_ Redis_ Redis publish subscription
14_Redis_乐观锁
Leetcode skimming -- sum of two integers 371 medium
LeetCode刷题——奇偶链表#328#Medium
高考录取分数线爬取
MySQL -- Index Optimization -- order by
Learn the method code example of converting timestamp to uppercase date using PHP
How to choose a third-party software testing organization for automated acceptance testing of mobile applications
17_Redis_Redis发布订阅
Pytoch saves tensor to Mat file
搭建自己的语义分割平台deeplabV3+
基于RZ/G2L | OK-G2LD-C开发板存储读写速度与网络实测
Facing the challenge of "lack of core", how can Feiling provide a stable and strong guarantee for customers' production capacity?
MD5加密
[solution] educational codeforces round 82