[LeetCode] 1. Two Sum_Easy tag: Hash Table
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
[LeetCode] 1. Two Sum_Easy tag: Hash Table的更多相关文章
- [LeetCode] 383. Ransom Note_Easy tag: Hash Table
Given an arbitrary ransom note string and another string containing letters from all the magazines, ...
- [LeetCode] 804. Unique Morse Code Words_Easy tag: Hash Table
International Morse Code defines a standard encoding where each letter is mapped to a series of dots ...
- [LeetCode] 884. Uncommon Words from Two Sentences_Easy tag: Hash Table
We are given two sentences A and B. (A sentence is a string of space separated words. Each word co ...
- [LeetCode] 532. K-diff Pairs in an Array_Easy tag: Hash Table
Given an array of integers and an integer k, you need to find the number of unique k-diff pairs in t ...
- [LeetCode] 697. Degree of an Array_Easy tag: Hash Table
Given a non-empty array of non-negative integers nums, the degree of this array is defined as the ma ...
- [LeetCode] 112. Path Sum_Easy tag: DFS
Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all ...
- Leetcode Tags(5)Hash Table
一.500. Keyboard Row 给定一个单词列表,只返回可以使用在键盘同一行的字母打印出来的单词. 输入: ["Hello", "Alaska", &q ...
- 算法与数据结构基础 - 哈希表(Hash Table)
Hash Table基础 哈希表(Hash Table)是常用的数据结构,其运用哈希函数(hash function)实现映射,内部使用开放定址.拉链法等方式解决哈希冲突,使得读写时间复杂度平均为O( ...
- 散列表(hash table)——算法导论(13)
1. 引言 许多应用都需要动态集合结构,它至少需要支持Insert,search和delete字典操作.散列表(hash table)是实现字典操作的一种有效的数据结构. 2. 直接寻址表 在介绍散列 ...
随机推荐
- 【Linux】使用 telnet 提示 Escape character is '^]'的意义
在linux/unix下使用telnet hostname port连接上主机后会提示Escape character is '^]' 这个提示的意思是按Ctrl + ] 会呼出telnet的命令行, ...
- C++ sort函数用法 C中的qsort
需要包含#include <algorithm>MSDN中的定义: template<class RanIt> void sort(RanIt first, RanIt ...
- Android 控制闪光灯
首先闪光灯可以用android.hardware.camera来控制. 1.添加权限 <uses-permission android:name="android.permission ...
- 重建索引:ALTER INDEX..REBUILD ONLINE vs ALTER INDEX..REBUILD
什么时候需要重建索引 1. 删除的空间没有重用,导致 索引出现碎片 2. 删除大量的表数据后,空间没有重用,导致 索引"虚高" 3.索引的 clustering_facto 和表不 ...
- 以太网端口二种链路类型:Access 和Trunk
Access 类型的端口:只能属于1 个VLAN,一般用于连接计算机的端口: Trunk 类型的端口:可以允许多个VLAN 通过,可以接收和发送多个VLAN 的报文,一般用于交换机之间连接的端口 ...
- 2016中国app年度排行榜:十大行业、25个领域、Top 500 和2017趋势预测
本文为猎豹全球智库联合猎豹移动大数据平台libra.科技顶尖媒体36kr联合发布,如需转载必须在文章开头注明“来源:猎豹全球智库”和作者姓名,且不得更改或增删文中所有信息. 本文作者:猎豹全球智库 容 ...
- VS2003安装Opencv1.0 windows系统 win7
一.步骤 下载安装opencv1.0 安装文件我上传到百度网盘分享连接 http://pan.baidu.com/s/1o8na0aA 配置电脑windows环境变量 配置VS2003全局设置 ...
- HTML5 Canvas 画虚线组件
前段时间由于项目需要,用到了HTML5 Canvas画图,但是没有画虚线的方法,自己写了一个HTML5 画虚线的组件. dashedLine.js if (window.CanvasRendering ...
- libxml2简单的生成、解析操作
3. 简单xml操作例子 link:http://www.blogjava.net/wxb_nudt/archive/2007/11/18/161340.html 了解以上基本知识之后,就可以进行一些 ...
- git回退之前版本
所有没有 commit 的本地改动,都会随着 reset --hard 丢掉,无法恢复. 如果只是想回到 pull 之前当前分支所在的commit位置,则可以.比方说你在 master 分支上,可以用 ...