当前位置:网站首页>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)有关系的?

将列表转为字典和列表。

这个写法看不懂,先放下,看看以后能不能看懂。
边栏推荐
- [flask] response, session and message flashing
- Ordinary people end up in Global trade, and a new round of structural opportunities emerge
- TrueType字体文件提取关键信息
- About error 2003 (HY000): can't connect to MySQL server on 'localhost' (10061)
- UE4 unreal engine, editor basic application, usage skills (IV)
- Poj2315 football games
- 【Flask】官方教程(Tutorial)-part1:项目布局、应用程序设置、定义和访问数据库
- [network attack and defense training exercises]
- VMware Tools installation error: unable to automatically install vsock driver
- Leetcode1961. 检查字符串是否为数组前缀
猜你喜欢

3D vision - 4 Getting started with gesture recognition - using mediapipe includes single frame and real time video

C web page open WinForm exe

Cookie concept, basic use, principle, details and Chinese transmission

c#网页打开winform exe

插卡4G工业路由器充电桩智能柜专网视频监控4G转以太网转WiFi有线网速测试 软硬件定制

3D模型格式汇总

现货白银的一般操作方法
![[understanding of opportunity-39]: Guiguzi - Chapter 5 flying clamp - warning 2: there are six types of praise. Be careful to enjoy praise as fish enjoy bait.](/img/3c/ec97abfabecb3f0c821beb6cfe2983.jpg)
[understanding of opportunity-39]: Guiguzi - Chapter 5 flying clamp - warning 2: there are six types of praise. Be careful to enjoy praise as fish enjoy bait.

【SSRF-01】服务器端请求伪造漏洞原理及利用实例

MATLB|实时机会约束决策及其在电力系统中的应用
随机推荐
Leetcode skimming questions_ Invert vowels in a string
A Cooperative Approach to Particle Swarm Optimization
MySQL learning notes 2
Remember that a version of @nestjs/typeorm^8.1.4 cannot be obtained Env option problem
ClickOnce does not support request execution level 'requireAdministrator'
剑指 Offer 12. 矩阵中的路径
[flask] static file and template rendering
Win10 add file extension
A glimpse of spir-v
竞价推广流程
Leetcode1961. Check whether the string is an array prefix
[Yu Yue education] Liaoning Vocational College of Architecture Web server application development reference
Cadre du Paddle: aperçu du paddlelnp [bibliothèque de développement pour le traitement du langage naturel des rames volantes]
Condition and AQS principle
False breakthroughs in the trend of London Silver
Leetcode skimming questions_ Sum of squares
3D模型格式汇总
竞赛题 2022-6-26
正则表达式:示例(1)
Kotlin basics 1