当前位置:网站首页>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边栏推荐
- arduino uno错误分析avrdude: stk500_recv(): programmer is not responding
- 【软件工程之美 - 专栏笔记】“一问一答”第3期 | 18个软件开发常见问题解决策略
- 八大排序----------------冒泡排序
- 太原市公交路线爬取
- Mathematical modeling experience
- STM32: mcnamu wheel tracking task (library function program code)
- 【Leetcode刷题】数组2——二分查找
- scanBasePackages扫包范围配置
- Eight sorts ------------- heap sort
- Encapsulation - Super keyword
猜你喜欢

shell工具finalShell

Traditional model predictive control trajectory tracking - circular trajectory (function package has been updated)

Joiner.on和stream().map联合使用技巧

Based on STM32: couple interactive doll (design scheme + source code +3d drawing +ad circuit)

传统模型预测控制轨迹跟踪——波浪形轨迹(功能包已经更新)

LoRa开启物联网新时代-ASR6500S、ASR6501/6502、ASR6505、ASR6601

Markdown and typora

【软件工程之美 - 专栏笔记】19 | 作为程序员,你应该有产品意识

Ml8 self study notes

arduino uno错误分析avrdude: stk500_recv(): programmer is not responding
随机推荐
Maya ACES工作流程配置(Arnold 及 RedShift 贴图配置规范-还原出SP-Aces流程下贴图正确的效果) PS还原Aces流程下渲染的图
STM32 printf问题总结 semihosting microLIB理解
2022 spring move - core technology FPGA post technical aspects (one side experience)
Ml8 self study notes LDA principle formula derivation
Linked list -------------------------- tail insertion method
DP1332E多协议高度集成非接触式读写芯片
leetcode---技巧
leetcode刷题笔记 763.划分字母区间(中等)
LeetCode #189.轮转数组
【软件工程之美 - 专栏笔记】26 | 持续交付:如何做到随时发布新版本到生产环境?
Redshift还原SP效果 - SP贴图导出设置及贴图导入配置
单链表面试题
【软件工程之美 - 专栏笔记】“一问一答”第2期 | 30个软件开发常见问题解决策略
从头安装MYSQL(MYSQL安装文档-解压版)
【软件工程之美 - 专栏笔记】17 | 需求分析到底要分析什么?怎么分析?
抽象封装继承多态
SimpleFOC调参2-速度、位置控制
【Leetcode刷题】数组3——分治
Shell tool finalshell
简洁代码实现pdf转word文档