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. smarty直接在模板中格式化时间的方法

    smarty提供了一个获取时间戳的方法: <%$smarty.now%> 使用该方法获取到当时的时间戳之后,使用格式化修饰符data-format进行修饰: <%$smarty.no ...

  2. Redis学习笔记--Redis配置文件redis.conf参数配置详解

    ########################################## 常规 ########################################## daemonize n ...

  3. spring boot 单元测试,如何使用profile

    一.问题概述 spring boot项目.单元测试的时候,我发现,总是会使用application.properties的内容,而该文件里,一般是我的开发时候的配置. 比如上图中,dev是开发配置,p ...

  4. Cannot assign to read only property 'exports' of object '#<Object>'

    基本原因是import 和module.exports 是不能混用在.vue的文件里面的 webpack 2中不允许混用import和module.exports module.exports={ 改 ...

  5. C程序设计语言习题(1-12)

    统计行数.单词数,字符数的程序: #include<stdio.h> #define IN 1 /*在单词内*/ #define OUT 0 /*在单词外*/ int main() { i ...

  6. virgo-tomcat-server的生产环境线上配置与管理

    Virgo Tomcat Server简称VTS,VTS是一个应用服务器,它是轻量级, 模块化, 基于OSGi系统.与OSGi紧密结合并且可以开发bundles形式的Spring web apps应用 ...

  7. javaAgent 参数

    -javaagent 这个JVM参数是JDK 5引进的. Java -help的帮助里面写道: -javaagent:<jarpath>[=<options>] load Ja ...

  8. 什么是webpack?

    https://www.webpackjs.com/concepts/ https://webpack.github.io/ 本质上,webpack 是一个现代 JavaScript 应用程序的静态模 ...

  9. 为自定义的View添加长按事件

    以前开发画板组件时,要添加一个长按监听事件,这个画板实际上就是继承自View的一个自定义组件. 首先,设置好长按事件发生时要触发的操作: private class LongPressRunnable ...

  10. vue中打包生成可配置文件以便修改接口地址

    vue打包上传到服务器之后,如果数据接口域名发生变化,需要手动修改接口地址,在发布的时候也麻烦,于是.. 在打包之后如果有一个json配置文件以便修改那不是方便很多 在网上找了一些方法貌似都是异步请求 ...