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

最新文章

  1. ASP.NET Core 中间件Diagnostics使用
  2. [hdu 3376]Matrix Again
  3. DBCP连接Oracle,数据库重启后现OALL8 is in an inconsistent state异常
  4. 《OD大数据实战》MapReduce实战
  5. linux signal 用法和注意事项
  6. ORA-00257: archiver error. Connect internal only, until freed 错误的处理方法
  7. 一个C#多线程的工作队列
  8. C#正则表达式匹配任意字符
  9. URL参数中有 特殊符号或加密数据 的问题解决
  10. UI基础UIButton
  11. POJ 1065 Wooden Sticks#贪心+qsort用法
  12. ubuntu16.04+eigen3安装
  13. ASP.net 路径问题 详解
  14. 安装zkpython出错
  15. 点击超链接打开本地QQ
  16. Unity3D使用碰撞体做触发器实现简单的自己主动开门
  17. Chapter 3 Phenomenon——2
  18. Android学习笔记——关于onConfigurationChanged(转)
  19. 【WPF】ListBox使用UserContrl作为子控件,引入UserContrl界面
  20. 戴尔 Latiteude E7240 i7-4600U

热门文章

  1. Qt编写机房安全作业预警系统
  2. 安卓APP动态调试-IDA实用攻略
  3. Struts2之web元素访问与模板包含与默认Action使用
  4. 让google.com不跳转到google.com.hk
  5. Unity3D动作资源(AnimatinClip)优化
  6. CmD空格转义的三种方法,总有一种会解决问题
  7. 源代码的管理与在eclipse中使用maven进行代码测试
  8. easyui treegrid idField 所在属性中值有花括号(如Guid)当有鼠标事件时会报错,行记录一下
  9. POJ-1958 Strange Towers of Hanoi(线性动规)
  10. tkinter 进度条