当前位置:网站首页>Leetcode sum of two numbers
Leetcode sum of two numbers
2022-07-06 01:38:00 【East invincible is me】
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 .
You can assume that each input corresponds to only one answer . however , The same element in the array cannot be repeated in the answer .
You can return the answers in any order .
My answer
Example 1:
Input :nums = [2,7,11,15], target = 9
Output :[0,1]
explain : because nums[0] + nums[1] == 9 , return [0, 1] .
Example 2:
Input :nums = [3,2,4], target = 6
Output :[1,2]
Example 3:
Input :nums = [3,3], target = 6
Output :[0,1]
Tips :
2 <= nums.length <= 104
-109 <= nums[i] <= 109
-109 <= target <= 109
There will only be one valid answer
Advanced : You can come up with a time complexity less than O(n2) The algorithm of ?
My answer :
class Solution:
def twoSum(self,nums,target):
list1=[]
for x in nums:
for y in nums:
if (x+y==target):
list1.append(nums.index(x))
list1.append(nums.index(y))
return list1
The function I wrote , By traversing the list directly , Find the number first , Then enter the value index. Time complexity O(n Fang )
Input example 1 , Get the results you want . But it can be seen that , Yes, there is bug Of . If you enter an instance 2, Will be output [0,0] But this is not the desired result . so , When it comes to testing , You need to know the internal logic of the code , Otherwise, the selected test case data , The test passed . But the actual code bug Not tested .
The following solution , It won't exist index Repetitive questions . But the time complexity is still On Fang
class Solution:
def twoSum(self,nums,target):
list1=[]
for i in range(len(nums)-1):
for j in range(i+1,len(nums)):
if (nums[i]+nums[j]==target):
return[i,j]
return []
Solution 2 of the answer :
Hashtable

class Solution:
def twoSum(self, nums: List[int], target: int) -> List[int]:
hashtable = dict()
for i, num in enumerate(nums):
if target - num in hashtable:
return [hashtable[target - num], i]
hashtable[nums[i]] = i
return []
Python There is no hash in , A dictionary is equivalent to a hash .
But I don't understand it here ,hashtable It's an empty dictionary , How to follow enumerate(nums) Related ?

Turn lists into dictionaries and lists .

I can't understand this writing , Put it down first , See if you can understand it later .
边栏推荐
- 什么是弱引用?es6中有哪些弱引用数据类型?js中的弱引用是什么?
- 【Flask】官方教程(Tutorial)-part1:项目布局、应用程序设置、定义和访问数据库
- Folio.ink 免费、快速、易用的图片分享工具
- About error 2003 (HY000): can't connect to MySQL server on 'localhost' (10061)
- [flask] official tutorial -part2: Blueprint - view, template, static file
- Basic operations of databases and tables ----- unique constraints
- Leetcode skimming questions_ Verify palindrome string II
- Luo Gu P1170 Bugs Bunny and Hunter
- 500 lines of code to understand the principle of mecached cache client driver
- [Jiudu OJ 09] two points to find student information
猜你喜欢

UE4 unreal engine, editor basic application, usage skills (IV)
![[机缘参悟-39]:鬼谷子-第五飞箝篇 - 警示之二:赞美的六种类型,谨防享受赞美快感如同鱼儿享受诱饵。](/img/3c/ec97abfabecb3f0c821beb6cfe2983.jpg)
[机缘参悟-39]:鬼谷子-第五飞箝篇 - 警示之二:赞美的六种类型,谨防享受赞美快感如同鱼儿享受诱饵。

Docker compose配置MySQL并实现远程连接

黄金价格走势k线图如何看?

ORA-00030

MySQL learning notes 2

Une image! Pourquoi l'école t'a - t - elle appris à coder, mais pourquoi pas...
![[技术发展-28]:信息通信网大全、新的技术形态、信息通信行业高质量发展概览](/img/94/05b2ff62a8a11340cc94c69645db73.png)
[技术发展-28]:信息通信网大全、新的技术形态、信息通信行业高质量发展概览

About error 2003 (HY000): can't connect to MySQL server on 'localhost' (10061)

A Cooperative Approach to Particle Swarm Optimization
随机推荐
[detailed] several ways to quickly realize object mapping
[机缘参悟-39]:鬼谷子-第五飞箝篇 - 警示之二:赞美的六种类型,谨防享受赞美快感如同鱼儿享受诱饵。
Alibaba canal usage details (pit draining version)_ MySQL and ES data synchronization
C web page open WinForm exe
TrueType字体文件提取关键信息
leetcode刷题_平方数之和
[flask] response, session and message flashing
Leetcode skimming questions_ Verify palindrome string II
Leetcode 剑指 Offer 59 - II. 队列的最大值
[flask] official tutorial -part3: blog blueprint, project installability
Idea sets the default line break for global newly created files
[flask] static file and template rendering
Yii console method call, Yii console scheduled task
MySQL learning notes 2
Luo Gu P1170 Bugs Bunny and Hunter
How does Huawei enable debug and how to make an image port
Leetcode 208. Implement trie (prefix tree)
[flask] official tutorial -part2: Blueprint - view, template, static file
WGet: command line download tool
插卡4G工业路由器充电桩智能柜专网视频监控4G转以太网转WiFi有线网速测试 软硬件定制