LeetCode第1题:Two Sum

【Codesys】获取从站信息,判断是否从站断联,总线丢失 获取EtherCAT从站信息,判断是否从站断联,总线丢失 阅读详情

Given an array of integers, return indices of the two numbers such that they add up to a specific target.

You may assume that each input would have exactly one solution, and you may not use the same element twice.

Example:

Given nums = [2, 7, 11, 15], target = 9,

Because nums[0] + nums[1] = 2 + 7 = 9,
return [0, 1].

Analyze: 

  1. 注意题目里的假设:each input would have exactly one solution,因此输出答案是唯一的,不会出现[ 3, 4, 5, 6 ]这样的数组:(3+6 = 4+5 = 9),所以优先考虑的是一次遍历。
  2. 在一次遍历的同时,我们可以计算出这个数需要的另一个加数,记为checkNum,例如5的checkNum就是(9-5=)4。
  3. 判断当前数字是否在checkNum中,例如遍历到数字4的时候,发现有checkNum中恰好有一个4,则成功找到。

这里的难点就在于如何返回checkNum对应那个数字的下标,解决办法是:

创建一个solution_dict字典,字典的key是checkNum,value是当前数字在nums中的下标

Code:

class Solution:
    def twoSum(self, nums: List[int], target: int) -> List[int]:
        solution_dict = {}
        for i in range(len(nums)):
            if (nums[i] in solution_dict):  # if already exists
                return [solution_dict[nums[i]],i]
            else:   # renew the dictionary
                # calcu what we need 
                checkNum = target - nums[i]
                solution_dict[checkNum] = i

Result:

Runtime: 40 ms, faster than 86.49% of Python3 online submissions for Two Sum.

Memory Usage: 14.5 MB, less than 5.08% of Python3 online submissions for Two Sum.

Open3D 机载点云电力线提取【2025最新版】 传统方法实现的机载点云电力线分割提取。博客长期更新,本文最新更新时间为:2025年3月23日。 阅读详情

相关推荐

GIS小白也能看懂的实战:5步教你将ArcGIS里的等高线和水系完美导入CAD做规划图

本文详细介绍了如何将ArcGIS中的等高线和水系数据高效导入CAD,制作专业规划底图的5个关键步骤。从数据准备、ArcGIS处理到CAD优化,涵盖坐标系设置、数据裁剪、格式转换等实用技巧,特别适合设计规划领域的新手快速掌握GIS与CAD协同工作流程。

weixin_27966471的博客 426

LeetCode习题笔记——Two Sum

Two Sum是LeetCode中最简单的例题之一,我们今天就从这个系列的题目开始练手,练习coding的算法以及算法的解析。 我们先从第一个题目开始看起: Given an array of integers, return indices of the two numbers such that they add up to a specific target. You ma

Ragnaross的博客 390

反序列化漏洞详解

目录 一、什么是序列化和反序列化 二、什么是反序列化漏洞 三、序列化函数(serialize) 四、反序列化(unserialize) ​五、什么是PHP魔术方法 六、一些常见的魔术方法 七、魔术方法的利用 八、反序列化漏洞的利用 1.__destruct()函数 2.__wakeup() 3.toString() ​九、反序列化漏洞的防御 一、什么是序列化和反序列化 序列化是将对象转换为字符串以便存储传输的一种方式。而反序列化恰好就是序列化的逆过程,反序列化会将字符串.

LJH1999ZN的博客 3万+

100天精刷LeetCode-Day1:经典Two Sum问题(附详细思路和python题解)

Day1 - Aug.22 Two Sum 问题: Given an array of integers, return indices of the two numbers such that they add up to a specific target. You may assume that each input would have exactly one solution, and you may not use the same element twice. Example Given n

Mochadrone的博客 545

SPSS(二)SPSS实现多因素方差分析模型(图文教程+数据集)

SPSS(二)SPSS实现多因素方差分析模型 单因素方差分析上一篇博客https://blog.youkuaiyun.com/LuYi_WeiLin/article/details/89917656已经介绍完毕 这篇博客我们主要来学习多因素方差分析 多因素方差分析,就是同时考虑若干个控制因素的情况下,分别分析它们的改变是否造成观察变量的显著变动 (多个自变量,一个因变量)自变量类型以分类变量为主也可以...

可乐联盟 6万+

【计算机图形学】Pov-Ray画的垃圾小车

效果: 代码: #include "colors.inc" #include "metals.inc" #include "glass.inc" //相机 //这个位置车子刚好落在草地上了 camera { location direction z*1.5 right x*image_width/image_height look_at

lishichengyan的博客 1749

LeetCode算法题解法体会

1.two sum Given an array of integers, return indices of the two numbers such that they add up to a specific target. You may assume that each input would have exactly one solution, and you may not

xiao 壕 的博客 332

深入解析DDR5 SDRAM中的CA训练模式(CATM):原理与实现

本文深入解析DDR5 SDRAM中的CA训练模式(CATM),详细探讨其工作原理与实现方法。通过分析JESD79-5标准,揭示CATM在时序校准、信号质量优化和系统稳定性方面的核心价值,并提供硬件设计考量和系统级调试技巧,帮助工程师解决高速内存系统中的信号完整性问题。

weixin_29235923的博客 383

关于一道题目解法

题目: Given an array of integers, return indices of the two numbers such that they add up to a specific target. You may assume that each input would have exactly one solution. Example: Given nums =

qq_33544335的博客 1716

使用 Simulink 进行电力电子系统的小信号模型仿真

电力电子系统(Power Electronics System)包括各种功率转换器(如 DC-DC 转换器、逆变器等)。这些设备中的关键组件(如 MOSFET、IGBT、电容器、电感器等)不仅影响系统的性能,还直接关系到系统的稳定性、效率和可靠性。因此,在设计阶段进行详细的建模和仿真至关重要。小信号模型仿真的目的是通过线性化非线性系统来评估其动态特性,包括增益、相位裕度、带宽等指标。通过本教程,我们介绍了如何使用 Simulink 进行电力电子系统的小信号模型仿真。背景介绍。

xiaoheshang_123的博客 480

风电光伏功率预测竞赛精华总结:技术解析与实战经验

本文深度解析新能源功率预测竞赛的技术要点与实战经验。首先介绍了风电、光伏功率预测对电网调度优化、降低运营风险的重要性,以及多个竞赛的基本概况。在数据特征分析部分,详细探讨了气象预报数据和功率数据的特征,包括时间周期性、数据质量问题及处理方法。针对异常值检测、数据标准化和特征工程(如时间特征、统计聚合特征、风速风向特征和滞后特征)提供了具体的技术实现方案。文章为新能源功率预测提供了实用的技术指导,对相关竞赛和实际应用具有重要参考价值。

算法工程师 205

leetcode--TWO SUM延伸(假设答案不唯一、假设输入有相同数字)

leetcode中的TWO SUM对结果进行了限制,You may assume that each input would have exactly one solution. 但是考虑实际情况,有可能在数列中,有多个组合的sum等于target。需要找到所有的情况。 1,  假设:在输入的数列中没有重复的数字,结果中数对不唯一 sort之后不跳出循环,接着寻找对应的数对。直到寻找完 v

u010523205的专栏 1850

数学建模模型总结

这是本人在两年内准备参加数学建模竞赛搜集到的模型资料,已经经过了本人的筛选和整理,希望能够对准备参加数学建模竞赛的学弟学妹们提供一些帮助!

【Java版】LeetCode 力扣第 1 题:两数之和 (two sum)

题目描述 给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 两个 整数,并返回他们的数组下标。 你可以假设每种输入只会对应一个答案。但是,你不能重复利用这个数组中同样的元素。 示例: 给定 nums = [2, 7, 11, 15], target = 9 因为 nums[0] + nums[1] = 2 + 7 = 9 所以返回 [0, 1] 题目解析 使用查找表来解决该问题。 设置一个 map 容器 record 用来记录元素的值与索引,然后遍历数组 nums

代码时光 384

华为的C语言编程规范.pdf

华为技术有限公司内部技术规范。本规范制定了编写C语言程序的基本原则、规则和建议。从代码的清晰、简洁、可测试、安全、程序效率、可移植各个方面对C语言编程作出了具体指导。

蓝牙模块HM-10手册

蓝牙模块HM-10手册,该手册对蓝牙模块HM-10进行了详细的介绍和使用方法以及一些AT指令。

【LeetCode】LeetCode——第1题:Two Sum

Given an array of integers, find two numbers such that they add up to a specific target number. The function twoSum should return indices of the two numbers such that they add up to the target, where index1 must be less than index2. Please note that your r

hujingshuang 5171

android 蓝牙通讯demo

android 下蓝牙通讯demo,实现了发现蓝牙,使能蓝牙,通过蓝牙发送消息。

leetcode题解:第1题Two Sum

https://leetcode-cn.com/problems/two-sum/ 分析 题目意思很明确,不考虑算法效率的话,这道题可以很容易用暴力解法做出来,两层循环获取两个整数,判断和是否等于目标值即可,时间复杂度为O(n2)O(n^2)O(n2)。 解法 为了得到更好的算法,我们转变思路,不是直接找两个整数然后判断它们的和,而是考虑对一个整数n,判断target-n是否在数组中,如果在,答案就是它们俩的下标。 判断一个数是否存在于数组中的最好办法就是哈希表,所以我们要将数组转变为哈希表,key为数组元

chenf1999的博客 349

[东哥的leetcode刷题日记] leetcode 1 :Two Sum

leetcode 1 :Two Sum 题目链接: https://leetcode-cn.com/problems/two-sum/ 难度: 简单 归类 : 数组操作,hashmap 题目 给定一个整数数组 nums和一个目标值 target,请你在该数组中找出和为目标值的那两个整数,并返回他们的数组下标。你可以假设每种输入只会对应一个答案。但是,数组中同一个元素不能使用两遍。 示例: 给定 nums = [2, 7, 11, 15], target = 9 因为 nums[0] + num

qq_41847324的博客 340

leetcode two sum python_LeetCode 第1题:Two Sum

题目给出一个整数数组,找出其中的两个整数,使相加等于指定整数,并返回这两个整数的索引。假设每组输入只有一种解法,并且不能重复使用同一个元素。举例:给出 nums = [2, 7, 11, 15], target = 9,因为 nums[0] + nums[1] = 2 + 7 = 9,所以返回 [0, 1].思路最简单也是最直接的想法就是两重循环直接遍历计算就能得到结果,时间复杂度为。但这肯定不是...

weixin_39995108的博客 184

Leetcode刷题实战(1):Two Sum

Leetcode不需要过多介绍了,今天一边开始刷题一边开始总结: 官网链接如下:https://leetcode.com/problemset/all/ 题1描述: 1 Two Sum 38.80% Easy Given an array of integers, return indices of the two numbers such that they add ...

10点43的博客 355

leetcode two sum python_LeetCode专题-Python实现之第1题:Two Sum

相关代码已经上传到github:https://github.com/exploitht/leetcode-python文中代码为了不动官网提供的初始几行代码内容,有一些不规范的地方,比如函数名大小写问题等等;更合理的代码实现参考我的github repo1、读题Given an array of integers, return indices of the two numbers such t...

weixin_39612726的博客 139

【LeetCode & 剑指offer刷题】数组题1:Two Sum(系列)

【LeetCode & 剑指offer刷题】数组题1:Two Sum(系列)   【LeetCode & 剑指offer 刷题笔记】目录(持续更新中...)   1. Two Sum Given an array of integers, return indices of the two numbers such that...

技术改变世界 6486

C++ leetcode刷题复盘1: two sum

代码参考:https://www.cnblogs.com/grandyang/p/4130379.html 相关学习基础知识参考: 1.C/C++ 定义向量、赋值和使用 2.unordered_map原理 3.unordered_map使用 解法1 使用暴力解法,相关有vector的定义与赋值。 class Solution { public: vector<int> twoSum(vector<int>& nums, int target) { ve

Amberfd的博客 450

LeetCode第1题:Two_Sum

Question: 1.Given an array of integers, returnindicesof the two numbers such that they add up to a specific target. 2.You may assume that each input would haveexactlyone solution, and you may no...

weixin_41502937的博客 145

LeetCode刷题:第1题Two Sum

听从老姐的建议,开始在LeetCode上刷题,争取每周能保质保量的完成2到3道题,针对有技巧的题目,我会以博客的形式将其中的原理以及各种算法都展现出来。所有题目请参考我的GitHub:GitHub地址 **题目要求:**Given an array of integers, return indices of the two numbers such that they ad...

CliuGeek的博客 501

山菜在刷题1《LeetCode1:two-sum》

给定一个整数数组 nums 和一个目标值 target,在该数组中找出和为目标值的那 两个 整数,并返回他们的数组下标。 你可以假设每种输入只会对应一个答案。但是,你不能重复利用这个数组中同样的元素。 示例: 给定 nums = [2, 7, 11, 15], target = 9 因为 nums[0] + nums[1] = 2 + 7 = 9 所以返回 [0, 1] 来源:力扣(LeetCod...

qq_35712608的博客 196

# LeetCode刷题:1.Two Sum

第一遍,读题不仔细,直接循环的nums中的元素,既出现了index out of bounds也无法得到正确答案。第二遍:忽略了每个元素只能用一次的条件,在循环中加入判断语句忽略数组下标相同的情况。若两元素之和为target,则储存两个下标并返回数组。若出现下标相同的情况,则直接忽略。

J_Felix的博客 233

LeetCode 第1题:Two Sum

Given an array of integers, return indices of the two numbers such that they add up to a specific target.You may assume that each input would have exactly one solution, and you may not use the same ele

perry_Fan 406
下一篇: 用Python判断手机号码的运营商
刘凯KAI
博客等级 码龄15年 663粉丝 128原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值