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

将列表转为字典和列表。

这个写法看不懂,先放下,看看以后能不能看懂。
边栏推荐
- XSS learning XSS lab problem solution
- How does Huawei enable debug and how to make an image port
- 【Flask】响应、session与Message Flashing
- Paddle框架:PaddleNLP概述【飛槳自然語言處理開發庫】
- A Cooperative Approach to Particle Swarm Optimization
- 干货!通过软硬件协同设计加速稀疏神经网络
- General operation method of spot Silver
- Accelerating spark data access with alluxio in kubernetes
- 500 lines of code to understand the principle of mecached cache client driver
- Docker compose configures MySQL and realizes remote connection
猜你喜欢

selenium 等待方式

【已解决】如何生成漂亮的静态文档说明页
![[technology development -28]: overview of information and communication network, new technology forms, high-quality development of information and communication industry](/img/94/05b2ff62a8a11340cc94c69645db73.png)
[technology development -28]: overview of information and communication network, new technology forms, high-quality development of information and communication industry
![[Jiudu OJ 09] two points to find student information](/img/35/25aac51fa3e08558b1f6e2541762b6.jpg)
[Jiudu OJ 09] two points to find student information

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

leetcode刷题_平方数之和

国家级非遗传承人高清旺《四大美人》皮影数字藏品惊艳亮相!

Huawei converged VLAN principle and configuration

VMware Tools installation error: unable to automatically install vsock driver

Poj2315 football games
随机推荐
Basic operations of databases and tables ----- default constraints
MATLB | real time opportunity constrained decision making and its application in power system
How to upgrade kubernetes in place
Flutter Doctor:Xcode 安装不完整
WGet: command line download tool
Ordinary people end up in Global trade, and a new round of structural opportunities emerge
Leetcode 剑指 Offer 59 - II. 队列的最大值
竞赛题 2022-6-26
[network attack and defense training exercises]
什么是弱引用?es6中有哪些弱引用数据类型?js中的弱引用是什么?
How to get all sequences in Oracle database- How can I get all sequences in an Oracle database?
2022年广西自治区中职组“网络空间安全”赛题及赛题解析(超详细)
Docker compose配置MySQL并实现远程连接
General operation method of spot Silver
C web page open WinForm exe
Spir - V premier aperçu
Basic operations of databases and tables ----- non empty constraints
[flask] response, session and message flashing
Paddle框架:PaddleNLP概述【飛槳自然語言處理開發庫】
ORA-00030