当前位置:网站首页>[leetcode] 167 - sum of two numbers II - enter an ordered array
[leetcode] 167 - sum of two numbers II - enter an ordered array
2022-07-02 15:36:00 【Crisp ~】
I'll give you a subscript from 1 The starting array of integers numbers , The array has been pressed Non decreasing order , Please find out from the array that the sum satisfying the addition is equal to the target number target Two numbers of . Let these two numbers be numbers[index1] and numbers[index2] , be 1 <= index1 < index2 <= numbers.length .
In length 2 Array of integers for [index1, index2] Returns the subscript of these two integers in the form of index1 and index2.
You can assume that each input Only corresponding to the only answer , And you Can not be Reuse the same elements .
The solution you design must use only constant level extra space .
Example 1:
Input : numbers = [2,7,11,15], target = 9
Output : [1,2]
explain : 2 And 7 The sum is equal to the number of targets 9 . therefore index1 = 1, index2 = 2 . return [1, 2] .
Example 2:
Input : numbers = [2,3,4], target = 6
Output : [1,3]
explain : 2 And 4 The sum is equal to the number of targets 6 . therefore index1 = 1, index2 = 3 . return [1, 3] .
Example 3:
Input : numbers = [-1,0], target = -1
Output : [1,2]
explain : -1 And 0 The sum is equal to the number of targets -1 . therefore index1 = 1, index2 = 2 . return [1, 2] .
Tips :
- 2 <= numbers.length <= 3 * 104
- -1000 <= numbers[i] <= 1000
- numbers Press Non decreasing order array
- -1000 <= target <= 1000
- There is only one valid answer
# The first is to use double circulation , Sure enough, it timed out
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
# Then replace the second level loop with binary search :
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]
# Double pointer
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]
边栏推荐
- LeetCode_ Sliding window_ Medium_ 395. Longest substring with at least k repeated characters
- 【LeetCode】189-轮转数组
- Practice of compiling principle course -- implementing an interpreter or compiler of elementary function operation language
- Redux - detailed explanation
- How to choose a third-party software testing organization for automated acceptance testing of mobile applications
- 【网络安全】网络资产收集
- 【LeetCode】1140-石子游戏II
- 02.面向容器化后,必须面对golang
- 【LeetCode】486-预测赢家
- LeetCode刷题——去除重复字母#316#Medium
猜你喜欢

4. Jctree related knowledge learning

FPGA - clock-03-clock management module (CMT) of internal structure of 7 Series FPGA

. Net again! Happy 20th birthday

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

. Solution to the problem of Chinese garbled code when net core reads files

LeetCode刷题——验证二叉树的前序序列化#331#Medium

Markdown tutorial

Download blender on Alibaba cloud image station

搭载TI AM62x处理器,飞凌FET6254-C核心板首发上市!

损失函数与正负样本分配:YOLO系列
随机推荐
Real estate market trend outlook in 2022
【LeetCode】695-岛屿的最大面积
Basic knowledge of cryptography
怎样从微信返回的json字符串中截取某个key的值?
17_Redis_Redis发布订阅
Bing. Site Internet
Redux - detailed explanation
【LeetCode】1254-统计封闭岛屿的数量
【LeetCode】1162-地图分析
folium地图无法显示的问题,临时性解决方案如下
Wechat Alipay account system and payment interface business process
04_ 栈
【LeetCode】1020-飞地的数量
彻底弄懂浏览器强缓存和协商缓存
Build your own semantic segmentation platform deeplabv3+
【LeetCode】283-移动零
. Solution to the problem of Chinese garbled code when net core reads files
Custom exception
5. Practice: jctree implements the annotation processor at compile time
2022 年辽宁省大学生数学建模A、B、C题(相关论文及模型程序代码网盘下载)