由于题目说了有且只有唯一解,可以考虑两遍扫描求解:第一遍扫描原数组,将所有的数重新存放到一个dict中,该dict以原数组中的值为键,原数组中的下标为值;第二遍扫描原数组,对于每个数nums[i]查看target-nums[i]是否在dict中,若在则可得到结果。 
当然,上面两遍扫描是不必要的,一遍即可,详见代码。

class Solution(object):
def twoSum(self, nums, target):
"""
:type nums: List[int]
:type target: int
:rtype: List[int]
"""
keys = {}
for i in xrange(len(nums)):
if target - nums[i] in keys:
return [keys[target - nums[i]], i]
if nums[i] not in keys:
keys[nums[i]] = i

Two Sum [easy] (Python)的更多相关文章

  1. LeetCode--Array--Two sum (Easy)

    1.Two sum (Easy)# Given an array of integers, return indices of the two numbers such that they add u ...

  2. 【leetcode】Minimum Path Sum(easy)

    Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right which ...

  3. 【leetcode】Two Sum (easy)

    Given an array of integers, find two numbers such that they add up to a specific target number. The ...

  4. [leetcode]Path Sum II @ Python

    原题地址:https://oj.leetcode.com/problems/path-sum-ii/ 题意: Given a binary tree and a sum, find all root- ...

  5. leetcode:Path Sum【Python版】

    1.类中递归调用函数需要加self # Definition for a binary tree node # class TreeNode: # def __init__(self, x): # s ...

  6. leetcode 【 Minimum Path Sum 】python 实现

    题目: Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right w ...

  7. [leetcode] #112 Path Sum (easy)

    原题链接 题意: 给定一个值,求出从树顶到某个叶(没有子节点)有没有一条路径等于该值. 思路: DFS Runtime: 4 ms, faster than 100.00% of C++ class ...

  8. leetcode Combination Sum II python

    Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in ...

  9. Leetcode——Two Sum(easy)

    题目:Given nums = [2, 7, 11, 15], target = 9, Because nums[0] + nums[1] = 2 + 7 = 9, return [0, 1] 代码: ...

随机推荐

  1. Office Web APP预览如何去掉顶部版权标志“Microsoft Office Web Apps”

    在Office Web APP的预览会涉及4中类型的文 件:Word.Excel.PowerPoint.PDF,不同的类型在预览时调用的文件是不一样的,其中Word和 PDF调用的是同一个文件.每个预 ...

  2. jstl c

    <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%> 例子:list中有两 ...

  3. 福大软工1816|K班—alpha冲刺

    Part.1 开篇 队名:彳艮彳亍团队 组长博客:戳我进入 作业博客:班级博客本次作业的链接 Part.2 成员汇报 组员1(组长)柯奇豪 过去两天完成了哪些任务 了解前端方面的相关内容,便于后续对进 ...

  4. 通过vb.net 和NPOI实现对excel的读操作

    通过vb.net 和NPOI实现对excel的读操作,很久很久前用过vb,这次朋友的代码是vb.net写的需要一个excel的操作, 就顾着着实现功能了,大家凑合着看吧 Option Explicit ...

  5. js 判断今天是否上班

    <script> var weekdate= getWeekDate() switch(weekdate){ case "星期一":; case "星期二&q ...

  6. python 爬虫proxy,BeautifulSoup+requests+mysql 爬取样例

    实现思路: 由于反扒机制,所以需要做代理切换,去爬取,内容通过BeautifulSoup去解析,最后入mysql库 1.在西刺免费代理网获取代理ip,并自我检测是否可用 2.根据获取的可用代理ip去发 ...

  7. Java集合类总结 (一)

    集合类中的基本接口 集合类中最基础的接口是Collection: public interface Collection<E> { boolean add(E element); Iter ...

  8. ASP.NET Session原理及处理方法

    session是怎么存储,提取的 1.在服务器端有一个session池,用来存储每个用户提交session中的数据,Session对于每一个客户端(或者说浏览器实例)是“人手一份”,用户首次与Web服 ...

  9. 一个简单的tcp代理实现

    There are a number of reasons to have a TCP proxy in your tool belt, bothfor forwarding traffic to b ...

  10. LoadRunner--获取请求的返回结果函数

    注:内容来自网络 Action(){ web_set_max_html_param_len("262144"); // 默认最大长度为256 web_reg_save_param( ...