当前位置:网站首页>leetcode-两数之和
leetcode-两数之和
2022-07-06 01:36:00 【东方不败就是我】
给定一个整数数组 nums 和一个整数目标值 target,请你在该数组中找出 和为目标值 target 的那 两个 整数,并返回它们的数组下标。
你可以假设每种输入只会对应一个答案。但是,数组中同一个元素在答案里不能重复出现。
你可以按任意顺序返回答案。
我的答案
示例 1:
输入:nums = [2,7,11,15], target = 9
输出:[0,1]
解释:因为 nums[0] + nums[1] == 9 ,返回 [0, 1] 。
示例 2:
输入:nums = [3,2,4], target = 6
输出:[1,2]
示例 3:
输入:nums = [3,3], target = 6
输出:[0,1]
提示:
2 <= nums.length <= 104
-109 <= nums[i] <= 109
-109 <= target <= 109
只会存在一个有效答案
进阶:你可以想出一个时间复杂度小于 O(n2) 的算法吗?
我的答案:
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
我写的这个函数,通过直接遍历列表,先找到数,再输入数值的index。时间复杂度O(n方)
输入示例一,得到了想要的结果。但是可以看出来,是有bug的。如果输入实例2,会输出[0,0]但这不是想要的结果。可见,测试的时候,需要知道代码的内在逻辑,否则选取的测试用例数据,测试通过。但实际代码的bug未测试出来。
如下这个解法,就不会存在index重复的问题。但时间复杂度仍是On方
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 []
答案的解法二:
哈希表

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中没有哈希,字典相当于哈希。
但这里没看懂,hashtable是个空字典,怎么跟enumerate(nums)有关系的?

将列表转为字典和列表。

这个写法看不懂,先放下,看看以后能不能看懂。
边栏推荐
- ORA-00030
- How to get all sequences in Oracle database- How can I get all sequences in an Oracle database?
- Electrical data | IEEE118 (including wind and solar energy)
- leetcode刷题_验证回文字符串 Ⅱ
- [ssrf-01] principle and utilization examples of server-side Request Forgery vulnerability
- LeetCode 322. Change exchange (dynamic planning)
- A Cooperative Approach to Particle Swarm Optimization
- Unity VR solves the problem that the handle ray keeps flashing after touching the button of the UI
- [le plus complet du réseau] | interprétation complète de MySQL explicite
- Loop structure of program (for loop)
猜你喜欢

Superfluid_ HQ hacked analysis

Poj2315 football games

Threedposetracker project resolution

Alibaba-Canal使用详解(排坑版)_MySQL与ES数据同步

Kubernetes stateless application expansion and contraction capacity

Docker compose configures MySQL and realizes remote connection

Yii console method call, Yii console scheduled task

一圖看懂!為什麼學校教了你Coding但還是不會的原因...

干货!通过软硬件协同设计加速稀疏神经网络

Win10 add file extension
随机推荐
ORA-00030
2022年广西自治区中职组“网络空间安全”赛题及赛题解析(超详细)
Mongodb problem set
Alibaba-Canal使用详解(排坑版)_MySQL与ES数据同步
[ssrf-01] principle and utilization examples of server-side Request Forgery vulnerability
基於DVWA的文件上傳漏洞測試
yii中console方法调用,yii console定时任务
2022年PMP项目管理考试敏捷知识点(8)
National intangible cultural heritage inheritor HD Wang's shadow digital collection of "Four Beauties" made an amazing debut!
[the most complete in the whole network] |mysql explain full interpretation
Cookie concept, basic use, principle, details and Chinese transmission
黄金价格走势k线图如何看?
正则表达式:示例(1)
Ordinary people end up in Global trade, and a new round of structural opportunities emerge
Huawei Hrbrid interface and VLAN division based on IP
VMware Tools installation error: unable to automatically install vsock driver
Yii console method call, Yii console scheduled task
[flask] obtain request information, redirect and error handling
Huawei converged VLAN principle and configuration
Loop structure of program (for loop)