当前位置:网站首页>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)有关系的?
将列表转为字典和列表。
这个写法看不懂,先放下,看看以后能不能看懂。
边栏推荐
- [technology development -28]: overview of information and communication network, new technology forms, high-quality development of information and communication industry
- Basic operations of databases and tables ----- primary key constraints
- Remember that a version of @nestjs/typeorm^8.1.4 cannot be obtained Env option problem
- Leetcode skimming questions_ Verify palindrome string II
- Huawei converged VLAN principle and configuration
- [flask] official tutorial -part3: blog blueprint, project installability
- [flask] static file and template rendering
- dried food! Accelerating sparse neural network through hardware and software co design
- Docker compose配置MySQL并实现远程连接
- [机缘参悟-39]:鬼谷子-第五飞箝篇 - 警示之二:赞美的六种类型,谨防享受赞美快感如同鱼儿享受诱饵。
猜你喜欢
Maya hollowed out modeling
现货白银的一般操作方法
Yii console method call, Yii console scheduled task
Superfluid_ HQ hacked analysis
插卡4G工业路由器充电桩智能柜专网视频监控4G转以太网转WiFi有线网速测试 软硬件定制
How does the crystal oscillator vibrate?
Basic operations of databases and tables ----- unique constraints
[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.
Loop structure of program (for loop)
Force buckle 9 palindromes
随机推荐
[technology development -28]: overview of information and communication network, new technology forms, high-quality development of information and communication industry
leetcode刷题_平方数之和
1. Introduction to basic functions of power query
MySQL learning notes 2
[flask] obtain request information, redirect and error handling
02.Go语言开发环境配置
Leetcode skimming questions_ Sum of squares
【详细】快速实现对象映射的几种方式
【Flask】官方教程(Tutorial)-part1:项目布局、应用程序设置、定义和访问数据库
一圖看懂!為什麼學校教了你Coding但還是不會的原因...
插卡4G工业路由器充电桩智能柜专网视频监控4G转以太网转WiFi有线网速测试 软硬件定制
Luo Gu P1170 Bugs Bunny and Hunter
Nmap: network detection tool and security / port scanner
File upload vulnerability test based on DVWA
ctf. Show PHP feature (89~110)
Docker compose configures MySQL and realizes remote connection
[network attack and defense training exercises]
【Flask】官方教程(Tutorial)-part2:蓝图-视图、模板、静态文件
Folio.ink 免费、快速、易用的图片分享工具
[Yu Yue education] Liaoning Vocational College of Architecture Web server application development reference