当前位置:网站首页>[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]
边栏推荐
- Storage read-write speed and network measurement based on rz/g2l | ok-g2ld-c development board
- Force deduction solution summary 2029 stone game IX
- 6.12 critical moment of Unified Process Platform
- Leetcode skimming -- verifying the preorder serialization of binary tree # 331 # medium
- MySQL calculate n-day retention rate
- Party History Documentary theme public welfare digital cultural and creative products officially launched
- Leetcode skimming -- count the number of numbers with different numbers 357 medium
- Application and practice of Jenkins pipeline
- Yolo format data set processing (XML to txt)
- 15_ Redis_ Redis. Conf detailed explanation
猜你喜欢
【网络安全】网络资产收集
Leetcode skimming - remove duplicate letters 316 medium
【LeetCode】1905-统计子岛屿
Let your HMI have more advantages. Fet-g2ld-c core board is a good choice
Set set you don't know
微信支付宝账户体系和支付接口业务流程
【LeetCode】417-太平洋大西洋水流问题
搭建自己的语义分割平台deeplabV3+
Guangzhou Emergency Management Bureau issued a high temperature and high humidity chemical safety reminder in July
Leetcode skimming -- incremental ternary subsequence 334 medium
随机推荐
党史纪实主题公益数字文创产品正式上线
搭建自己的语义分割平台deeplabV3+
11_ Redis_ Hyperloglog_ command
13_ Redis_ affair
【LeetCode】417-太平洋大西洋水流问题
夏季高考文化成绩一分一段表
03_线性表_链表
PTA 天梯赛习题集 L2-001 城市间紧急救援
12_Redis_Bitmap_命令
Yolov5 code reproduction and server operation
Be a good gatekeeper on the road of anti epidemic -- infrared thermal imaging temperature detection system based on rk3568
folium地图无法显示的问题,临时性解决方案如下
Beijing rental data analysis
工程师评测 | RK3568开发板上手测试
LeetCode刷题——两整数之和#371#Medium
Redux - detailed explanation
XML Configuration File
6.12 企业内部upp平台(Unified Process Platform)的关键一刻
How to find a sense of career direction
03.golang初步使用