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的更多相关文章

  1. [LeetCode] 383. Ransom Note_Easy tag: Hash Table

    Given an arbitrary ransom note string and another string containing letters from all the magazines, ...

  2. [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 ...

  3. [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 ...

  4. [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 ...

  5. [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 ...

  6. [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 ...

  7. Leetcode Tags(5)Hash Table

    一.500. Keyboard Row 给定一个单词列表,只返回可以使用在键盘同一行的字母打印出来的单词. 输入: ["Hello", "Alaska", &q ...

  8. 算法与数据结构基础 - 哈希表(Hash Table)

    Hash Table基础 哈希表(Hash Table)是常用的数据结构,其运用哈希函数(hash function)实现映射,内部使用开放定址.拉链法等方式解决哈希冲突,使得读写时间复杂度平均为O( ...

  9. 散列表(hash table)——算法导论(13)

    1. 引言 许多应用都需要动态集合结构,它至少需要支持Insert,search和delete字典操作.散列表(hash table)是实现字典操作的一种有效的数据结构. 2. 直接寻址表 在介绍散列 ...

随机推荐

  1. Eclipse中如何在指定工程中搜索指定的字符串

    1.点击Search: 2.在下拉框中先择Search. 3. 4. 5.选择Java 6.

  2. libevent安装方法

    安装FastDFS之前,先安装libevent工具包,记录一下安装过程 1.检查:ls -al /usr/lib | grep libevent 查看是否已安装,如果已安装且版本低于1.3,则先通过: ...

  3. 用Ant给Unity3D导出Eclipse工程打包APK

    我们经常需要出完apk后,告诉我们改版本号,或者包名什么的,但是每次打包时间又很长.索性我们就出一个eclipse工程,然后用ant自动打包. 1.设置环境变量 2.生成build.xml文件 and ...

  4. 提高VS2010运行速度的技巧

    任务管理器,CPU和内存都不高,为何?原因就是VS2010不停地读硬盘导致的; 写代码2/3的时间都耗在卡上了,太难受了; 研究发现,VS2010如果你装了VC等语言,那么它就会自动装SQL Serv ...

  5. 【CF913F】Strongly Connected Tournament 概率神题

    [CF913F]Strongly Connected Tournament 题意:有n个人进行如下锦标赛: 1.所有人都和所有其他的人进行一场比赛,其中标号为i的人打赢标号为j的人(i<j)的概 ...

  6. Android 应用内切换语言

    extends :http://bbs.51cto.com/thread-1075165-1.html,http://www.cnblogs.com/loulijun/p/3164746.html 1 ...

  7. linux安装环境

    我用的是Linux ubuntu 3.19.0-25-generic #26~14.04.1-Ubuntu SMP Fri Jul 24 21:16:20 UTC 2015 x86_64 x86_64 ...

  8. 51单片机之IIC通信原理及软件仿真

    关于IIC我觉这个博客里面说的已经够清楚了 如下图所示的写操作的时序图: 其实像这种通信协议的要求是很精确的,一点点不对都可能导致在实际工程中无法读取数据.我就是被一个应答位耽误了好久,还好最后被我发 ...

  9. 算法学习之冒泡排序的C实现

    冒泡排序是属于比较类的排序方式,简单易懂,但是效率不是很高,不及快排. #include "stdio.h" #include "time.h" void bu ...

  10. AppScan扫描建议 问题集

    1.1        AppScan扫描建议 若干问题的补救方法在于对用户输入进行清理. 通过验证用户输入未包含危险字符,便可能防止恶意的用户导致应用程序执行计划外的任务,例如:启动任意 SQL 查询 ...