当前位置:网站首页>Leetcode 167. sum of two numbers II - input ordered array
Leetcode 167. sum of two numbers II - input ordered array
2022-07-29 06:23:00 【Zhangchuming ZCM】
Power button | 167. Sum of two numbers II - Input an ordered array
Title screenshot

Method 1 : Violence law
The corresponding array can be found by two loops .
Violence law will fail the test because of overtime .
Be careful : The problem requires a unique solution , And the same subscript cannot be repeated , So when the answer is two same numbers , Must return even different subscripts . So the outer layer is set to i stay 0 To n Between , The inner layer is set to j stay i+1 To n Between .
class Solution:
def twoSum(self, numbers: List[int], target: int) -> List[int]:
ans = [0]*2
n = len(numbers)
for left in range(0, n):
for right in range(left+1, n):
if numbers[left]+numbers[right] == target:
ans=[left+1, right+1]
return ansComplete test code
from typing import List
class Solution:
def twoSum(self, numbers: List[int], target: int) -> List[int]:
ans = [0]*2
n = len(numbers)
for left in range(0, n):
for right in range(left+1, n):
if numbers[left]+numbers[right] == target:
ans=[left+1, right+1]
return ans
class main:
a = Solution()
numbers = [-1,0,4,4,6]
target = 8
b=a.twoSum(numbers, target)
print(b)
if __name__ == '__main__':
main()Method 2 : Two points search
It also uses the internal and external circulation , The difference is that the inner layer is dichotomy , In this way, the time complexity can be reduced .
The external circulation fixes the first number , Then the inner circle uses dichotomy to find the second number .
The first number is i, Left and right respectively i+1, and n-1. Then keep finding compromises .
class Solution:
def twoSum(self, numbers: List[int], target: int) -> List[int]:
n = len(numbers)
for i in range(0, n):
left, right = i + 1, n - 1
while left <= right:
mid = (left + right)//2
if numbers[mid] == target-numbers[i]:
return [i+1, mid+1]
elif numbers[mid] > target-numbers[i]:
right = mid - 1
else:
left = mid + 1Method 3 : Double pointer
Use double pointers directly , Move from both sides to the middle , Only one layer of circulation is needed . It greatly reduces the time complexity .
Set the left and right pointers to 0, and n-1, Move from both sides to the middle . If the sum value is found to be exactly equal to the target value , The subscript is returned . The subscript of the title is given from 1 Start , Computer subscript from 0 Start , Remember to subscript when returning +1.
If the sum value is found to be greater than the target value , Then the right pointer moves left . conversely , Move the pointer to the right .
class Solution:
def twoSum(self, numbers: List[int], target: int) -> List[int]:
n = len(numbers)
left, right = 0, n - 1
while left < right:
if numbers[left]+ numbers[right] == target:
return [left+1, right+1]
elif numbers[left]+ numbers[right] > target:
right -= 1
else:
left += 1边栏推荐
- 【软件工程之美 - 专栏笔记】24 | 技术债务:是继续修修补补凑合着用,还是推翻重来?
- 传统模型预测控制轨迹跟踪——波浪形轨迹(功能包已经更新)
- 三国演义章节内容
- Abstract classes and interfaces
- 【软件工程之美 - 专栏笔记】“一问一答”第2期 | 30个软件开发常见问题解决策略
- Computer factory interview questions
- 寒假集训总结 (1.23~1.28) [第一梯队]
- Eight sorts ------------- heap sort
- 【软件工程之美 - 专栏笔记】19 | 作为程序员,你应该有产品意识
- 从头安装MYSQL(MYSQL安装文档-解压版)
猜你喜欢
![[beauty of software engineering - column notes] 19 | as a programmer, you should have product awareness](/img/32/180bfe5904366946fca0d987f4e8ad.png)
[beauty of software engineering - column notes] 19 | as a programmer, you should have product awareness

JVM内存结构

2022 spring recruit - Hesai technology FPGA technology post (one or two sides, collected from: Digital IC workers and FPGA Explorers)

【软件工程之美 - 专栏笔记】16 | 怎样才能写好项目文档?

Ml self study notes 5

Ml6 self study notes

顺序表和链表

Sqlyog installation and configuration tutorial

Huawei cloud 14 day Hongmeng device development -day3 kernel development

HR must ask questions - how to fight with HR (collected from FPGA Explorer)
随机推荐
[beauty of software engineering - column notes] 20 | how to deal with the headache of requirement change?
【软件工程之美 - 专栏笔记】“一问一答”第3期 | 18个软件开发常见问题解决策略
Number theory: proof that the maximum number that px+py cannot represent is pq-p-q
MySql-面试题
TLE5012b+STM32F103C8T6(bluepill)读取角度数据
clickhouse 导入CSV失败 不报错但是无数据
八大排序----------------冒泡排序
Shell tool finalshell
基于F407ZGT6的WS2812B彩灯驱动
IDEA安装scala
滑动窗口 Leetcode 76.最小覆盖子串(困难) 76.76. MinimumWindow Substring (Hard)
Computer network interview questions
Ml4 self study notes
TB6600+stm32F407测试
唯美girls
Ml self study notes 5
SimpleFOC调参1-力矩控制
Joiner.on和stream().map联合使用技巧
Redshift还原SP效果 - SP贴图导出设置及贴图导入配置
【Leetcode刷题】数组2——二分查找