当前位置:网站首页>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)有关系的?
将列表转为字典和列表。
这个写法看不懂,先放下,看看以后能不能看懂。
边栏推荐
- C web page open WinForm exe
- 037 PHP login, registration, message, personal Center Design
- About error 2003 (HY000): can't connect to MySQL server on 'localhost' (10061)
- internship:项目代码所涉及陌生注解及其作用
- Loop structure of program (for loop)
- Paddle framework: paddlenlp overview [propeller natural language processing development library]
- 安装Redis
- Unity | two ways to realize facial drive
- [flask] static file and template rendering
- Leetcode 剑指 Offer 59 - II. 队列的最大值
猜你喜欢
Ordinary people end up in Global trade, and a new round of structural opportunities emerge
Basic process and testing idea of interface automation
Test de vulnérabilité de téléchargement de fichiers basé sur dvwa
dried food! Accelerating sparse neural network through hardware and software co design
【Flask】官方教程(Tutorial)-part3:blog蓝图、项目可安装化
【Flask】官方教程(Tutorial)-part2:蓝图-视图、模板、静态文件
Maya hollowed out modeling
Unity | two ways to realize facial drive
National intangible cultural heritage inheritor HD Wang's shadow digital collection of "Four Beauties" made an amazing debut!
Basic operations of databases and tables ----- unique constraints
随机推荐
Basic operations of databases and tables ----- default constraints
Unity | two ways to realize facial drive
Maya hollowed out modeling
01.Go语言介绍
Kotlin basics 1
Crawler request module
Flutter Doctor:Xcode 安装不完整
[Jiudu OJ 09] two points to find student information
selenium 元素定位(2)
Redis-Key的操作
A glimpse of spir-v
Leetcode1961. 检查字符串是否为数组前缀
Unreal browser plug-in
A Cooperative Approach to Particle Swarm Optimization
NLP第四范式:Prompt概述【Pre-train,Prompt(提示),Predict】【刘鹏飞】
Reasonable and sensible
A Cooperative Approach to Particle Swarm Optimization
竞赛题 2022-6-26
ClickOnce 不支持请求执行级别“requireAdministrator”
Leetcode 208. 实现 Trie (前缀树)