当前位置:网站首页>Sum the two numbers to find the target value
Sum the two numbers to find the target value
2022-07-02 10:21:00 【Lost ~ know to return】
Sum the two numbers to find the target value
subject : Sum of two numbers
Given an array of integers nums And an integer target value target, Please find... In the array And is the target value target the Two Integers , And return their array subscripts .
Their thinking
Find the sum of two numbers , Using fast and slow pointers , When two pointers When the extracted function value is added to the target value , Meet the conditions
The first solution : Violence enumeration
This solution is the easiest to come up with , But the memory consumption is too large
class Solution(object):
def twoSum(self, nums, target):
""" :type nums: List[int] :type target: int :rtype: List[int] """
L = []
n = len(nums)
for i in range(n-1):
for j in range(i+1, n):
if nums[i] + nums[j] == target:
L.append(i)
L.append(j)
j = j + 1
i = i + 1
return L
double for loop : Double pointers for convenient summation
Time complexity :n*n, Spatial complexity , Introduced a new array , by n
Code transformation
class Solution(object):
def twoSum(self, nums, target):
# Method 1. Convenient array , Enumeration , Time and space consumption is huge
n = len(nums)
for i in range(n):
for j in range(i+1, n):
if nums[i] + nums[j] == target:
return [i, j]
return []
Time complexity :n*n, Spatial complexity :1
Method 2 : Hashtable
Function method :
The method of all key value pairs in the convenience dictionary :
for...in... # Loop statements to facilitate all key value pairs
==enumerate()== Function instructions :
- enumerate() yes python Built in functions for
- enumerate In the dictionary is enumeration 、 List means
- For an iterative (iterable)/ Traversable objects ( As listing 、 character string ),enumerate Make it into an index sequence , Use it to get both index and value
- enumerate It's mostly used in for Count in the loop
# for example : Output the subscript corresponding to each character in the list
listx = [" this ", " yes ", " One ", " test "]
for i in range(len(listx)):
print i, listx[i]
# enumerate() Use of functions
listx = [" this ", " yes ", " One ", " test "]
for index, item in enumerate(listx):
print index, item
The output is as follows :
Therefore, this question can be modified by hash table :
Thought analysis :
The time of searching in the list can be reduced through hash table
class Solution(object):
def twoSum(self, nums, target):
# Method 2 : Hashtable ,set No, index,dict No, add attribute
visited = dict()
for i,num in enumerate(nums):
if (target-num) in visited:
return (visited[target-num], i)
visited[nums[i]] = i
return []
error analysis :
- Use set, however set() Cannot return subscript , Therefore adopt dict()
Time complexity :o(N), Spatial complexity o(1)
边栏推荐
- Alibaba cloud ack introduction
- A model can do two things: image annotation and image reading Q & A. VQA accuracy is close to human level | demo can be played
- Application of rxjs operator withlatestfrom in Spartacus UI of SAP e-commerce cloud
- Mobile mall app solution: how much is it to make an app? Detailed explanation of APP mall development content
- Introduction and Principle notes of UE4 material
- ue虛幻引擎程序化植物生成器設置——如何快速生成大片森林
- Skywalking理论与实践
- How much is it to develop a system software in Beijing, and what funds are needed to develop the software
- 【虚幻】自动门蓝图笔记
- 虛幻AI藍圖基礎筆記(萬字整理)
猜你喜欢
2837xd code generation module learning (1) -- GPIO module
High level application of SQL statements in MySQL database (II)
Introduction and Principle notes of UE4 material
Junit5 supports suite methods
Junit5 支持suite的方法
How to achieve the top progress bar effect in background management projects
It is the most difficult to teach AI to play iron fist frame by frame. Now arcade game lovers have something
Mysql索引
[ue5] blueprint making simple mine tutorial
What is the relationship between realizing page watermarking and mutationobserver?
随机推荐
Alibaba cloud SMS service
2837xd代码生成模块学习(1)——GPIO模块
Matlab代码生成之SIL/PIL测试
SAP Spartacus express checkout design
【虚幻4】UMG组件的简介与使用(更新中...)
Project practice, redis cluster technology learning (6)
Matlab生成dsp程序——官方例程学习(6)
Spatial interpretation | comprehensive analysis of spatial structure of primary liver cancer
It is the most difficult to teach AI to play iron fist frame by frame. Now arcade game lovers have something
2021-10-02
How much is it to develop a system software in Beijing, and what funds are needed to develop the software
Large neural networks may be beginning to realize: the chief scientist of openai leads to controversy, and everyone quarrels
[unreal] key to open the door blueprint notes
阿里云短信服务
Ue5 - AI pursuit (blueprint, behavior tree)
【MySQL】连接MySQL时出现异常:Connection must be valid and open
Eslint reports an error
The latest progress and development trend of 2022 intelligent voice technology
ue4材质的入门和原理笔记
Mysql索引