当前位置:网站首页>[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_宕机后手动配置主机
- Common English abbreviations for data analysis (I)
- Data analysis thinking analysis methods and business knowledge - business indicators
- 06_ Stack and queue conversion
- Pytoch saves tensor to Mat file
- Real estate market trend outlook in 2022
- XML Configuration File
- 18_ Redis_ Redis master-slave replication & cluster building
- 16_ Redis_ Redis persistence
- LeetCode刷题——验证二叉树的前序序列化#331#Medium
猜你喜欢
20_ Redis_ Sentinel mode
Loss function and positive and negative sample allocation: Yolo series
21_Redis_浅析Redis缓存穿透和雪崩
Semantic segmentation learning notes (1)
03. Preliminary use of golang
LeetCode刷题——奇偶链表#328#Medium
LeetCode刷题——验证二叉树的前序序列化#331#Medium
17_ Redis_ Redis publish subscription
PTA 天梯赛习题集 L2-001 城市间紧急救援
02_ Linear table_ Sequence table
随机推荐
yolo格式数据集处理(xml转txt)
03. Preliminary use of golang
20_Redis_哨兵模式
Leetcode skimming -- sum of two integers 371 medium
MD5加密
The traversal methods of binary tree mainly include: first order traversal, middle order traversal, second order traversal, and hierarchical traversal. First order, middle order, and second order actu
自定义异常
【LeetCode】876-链表的中间结点
【LeetCode】1020-飞地的数量
2022 college students in Liaoning Province mathematical modeling a, B, C questions (related papers and model program code online disk download)
. Solution to the problem of Chinese garbled code when net core reads files
How to avoid 7 common problems in mobile and network availability testing
Pytoch saves tensor to Mat file
Equipped with Ti am62x processor, Feiling fet6254-c core board is launched!
20_ Redis_ Sentinel mode
终于搞懂了JS中的事件循环,同步/异步,微任务/宏任务,运行机制(附笔试题)
[solution] educational codeforces round 82
. Net again! Happy 20th birthday
Bing.com網站
Wechat Alipay account system and payment interface business process