当前位置:网站首页>[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]
边栏推荐
- 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?
- Leetcode skimming -- incremental ternary subsequence 334 medium
- Beijing rental data analysis
- 终于搞懂了JS中的事件循环,同步/异步,微任务/宏任务,运行机制(附笔试题)
- 语义分割学习笔记(一)
- 【LeetCode】1020-飞地的数量
- There are 7 seats with great variety, Wuling Jiachen has outstanding product power, large humanized space, and the key price is really fragrant
- LeetCode刷题——两整数之和#371#Medium
- Yolov5 code reproduction and server operation
猜你喜欢

Guangzhou Emergency Management Bureau issued a high temperature and high humidity chemical safety reminder in July

损失函数与正负样本分配:YOLO系列

自定义异常

I made an istio workshop. This is the first introduction

Download blender on Alibaba cloud image station

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

6.12 企业内部upp平台(Unified Process Platform)的关键一刻

密码学基础知识

LeetCode刷题——去除重复字母#316#Medium

Custom exception
随机推荐
基于RZ/G2L | OK-G2LD-C开发板存储读写速度与网络实测
LeetCode_ String_ Simple_ 412.Fizz Buzz
【LeetCode】19-删除链表的倒数第N个结点
[solution] educational codeforces round 82
Be a good gatekeeper on the road of anti epidemic -- infrared thermal imaging temperature detection system based on rk3568
Custom exception
Steps for Navicat to create a new database
Real estate market trend outlook in 2022
10_Redis_geospatial_命令
Common English abbreviations for data analysis (I)
搭建自己的语义分割平台deeplabV3+
SQL stored procedure
12_ Redis_ Bitmap_ command
MySQL calculate n-day retention rate
Basic knowledge of cryptography
Mavn builds nexus private server
Download blender on Alibaba cloud image station
Redux——详解
[network security] network asset collection
Force deduction solution summary 2029 stone game IX