当前位置:网站首页>[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]
边栏推荐
- Steps for Navicat to create a new database
- Leetcode skimming -- sum of two integers 371 medium
- 6.12 企业内部upp平台(Unified Process Platform)的关键一刻
- Yolo format data set processing (XML to txt)
- 02_线性表_顺序表
- 面对“缺芯”挑战,飞凌如何为客户产能提供稳定强大的保障?
- 【LeetCode】189-轮转数组
- yolo格式数据集处理(xml转txt)
- 02. After containerization, you must face golang
- Oracle primary key auto increment
猜你喜欢

终于搞懂了JS中的事件循环,同步/异步,微任务/宏任务,运行机制(附笔试题)

List set & UML diagram

4. Jctree related knowledge learning

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

20_ Redis_ Sentinel mode

Data analysis thinking analysis methods and business knowledge - business indicators

16_ Redis_ Redis persistence

08_ strand

10_Redis_geospatial_命令

03_ Linear table_ Linked list
随机推荐
03_线性表_链表
. Solution to the problem of Chinese garbled code when net core reads files
Basic knowledge of cryptography
12_ Redis_ Bitmap_ command
[development environment] install the Chinese language pack for the 2013 version of visual studio community (install test agents 2013 | install visual studio 2013 simplified Chinese)
Bing. Site Internet
提前批院校名称
04_ Stack
Let your HMI have more advantages. Fet-g2ld-c core board is a good choice
yolo格式数据集处理(xml转txt)
语义分割学习笔记(一)
15_Redis_Redis.conf详解
05_ queue
Redux - detailed explanation
Evaluation of embedded rz/g2l processor core board and development board of Feiling
Application and practice of Jenkins pipeline
Storage read-write speed and network measurement based on rz/g2l | ok-g2ld-c development board
Oracle primary key auto increment
03_線性錶_鏈錶
Map introduction