当前位置:网站首页>[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]
边栏推荐
- 19_Redis_宕机后手动配置主机
- 4. Data splitting of Flink real-time project
- Summary of the first three passes of sqli Labs
- LeetCode_ String_ Simple_ 412.Fizz Buzz
- 党史纪实主题公益数字文创产品正式上线
- Bing.com網站
- Libcurl Lesson 13 static library introduces OpenSSL compilation dependency
- [solution] educational codeforces round 82
- 【LeetCode】417-太平洋大西洋水流问题
- How to choose a third-party software testing organization for automated acceptance testing of mobile applications
猜你喜欢

Let your HMI have more advantages. Fet-g2ld-c core board is a good choice

Storage read-write speed and network measurement based on rz/g2l | ok-g2ld-c development board

How to find a sense of career direction

15_Redis_Redis.conf详解

Solve the problem of frequent interruption of mobaxterm remote connection

Practical debugging skills

搭建自己的语义分割平台deeplabV3+

03_线性表_链表

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

Application and practice of Jenkins pipeline
随机推荐
How to avoid 7 common problems in mobile and network availability testing
Engineer evaluation | rk3568 development board hands-on test
02_线性表_顺序表
Data analysis thinking analysis methods and business knowledge - business indicators
【LeetCode】1254-统计封闭岛屿的数量
Steps for Navicat to create a new database
6.12 企业内部upp平台(Unified Process Platform)的关键一刻
党史纪实主题公益数字文创产品正式上线
工程师评测 | RK3568开发板上手测试
4. Data splitting of Flink real-time project
Build your own semantic segmentation platform deeplabv3+
[network security] network asset collection
高考录取分数线爬虫
04_ Stack
2022 college students in Liaoning Province mathematical modeling a, B, C questions (related papers and model program code online disk download)
Solution of Queen n problem
02. After containerization, you must face golang
Markdown tutorial
Practice of compiling principle course -- implementing an interpreter or compiler of elementary function operation language
Redux——详解