[LeetCode] 1. Two Sum_Easy tag: Hash Table
2024-10-18 12:51:36
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].
思路为hash table, 每次将target - num 在d里面找, 如果有, 返回两个index, 否则将d[index] = num 更新d.
1. Constraints
1) exactly one solution, 总是有解, 所以无edge case
2. Ideas
hash table T: O(n) S: O(n)
3. Code
class Solution:
def twoSum(self, nums, target):
d = {}
for index, num in enumerate(nums):
rem = target - num
if rem in d:
return [d[rem], index]
d[num] = index
最新文章
- ASP.NET Core 中间件Diagnostics使用
- [hdu 3376]Matrix Again
- DBCP连接Oracle,数据库重启后现OALL8 is in an inconsistent state异常
- 《OD大数据实战》MapReduce实战
- linux signal 用法和注意事项
- ORA-00257: archiver error. Connect internal only, until freed 错误的处理方法
- 一个C#多线程的工作队列
- C#正则表达式匹配任意字符
- URL参数中有 特殊符号或加密数据 的问题解决
- UI基础UIButton
- POJ 1065 Wooden Sticks#贪心+qsort用法
- ubuntu16.04+eigen3安装
- ASP.net 路径问题 详解
- 安装zkpython出错
- 点击超链接打开本地QQ
- Unity3D使用碰撞体做触发器实现简单的自己主动开门
- Chapter 3 Phenomenon——2
- Android学习笔记——关于onConfigurationChanged(转)
- 【WPF】ListBox使用UserContrl作为子控件,引入UserContrl界面
- 戴尔 Latiteude E7240 i7-4600U
热门文章
- Qt编写机房安全作业预警系统
- 安卓APP动态调试-IDA实用攻略
- Struts2之web元素访问与模板包含与默认Action使用
- 让google.com不跳转到google.com.hk
- Unity3D动作资源(AnimatinClip)优化
- CmD空格转义的三种方法,总有一种会解决问题
- 源代码的管理与在eclipse中使用maven进行代码测试
- easyui treegrid idField 所在属性中值有花括号(如Guid)当有鼠标事件时会报错,行记录一下
- POJ-1958 Strange Towers of Hanoi(线性动规)
- tkinter 进度条