由于题目说了有且只有唯一解,可以考虑两遍扫描求解:第一遍扫描原数组,将所有的数重新存放到一个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. Load-time relocation of shared libraries

    E原文地址:http://eli.thegreenplace.net/2011/08/25/load-time-relocation-of-shared-libraries/ This article ...

  2. java IO Nio 文件拷贝工具类Files

    public static void main(String[] args) throws Exception { Files.copy(Paths.get("file/text.txt&q ...

  3. 编写高质量代码改善C#程序的157个建议——建议3: 区别对待强制转型与as和is

    建议3: 区别对待强制转型与as和is 在阐述本建议之前,首先需要明确什么是强制转型,以及强制转型意味着什么.从语法结构上来看,类似下面的代码就是强制转型. secondType = (SecondT ...

  4. Spring MVC:Model、View、ModelAndView

    个人理解:View为服务器上的某个文件容器,可以为JSP,FTL等动态页面文件,甚至是媒体文件等等,单单是一个文件.Model的作用是存储动态页面属性,动态页面文件即View可以在Model中获取动态 ...

  5. ORACLE_EBS_R12_采购到入库所经历的表

    --采购到入库所经历的表 --0.请购单 --创建请购单方式有 --a.从外挂系统导入请购的接口表PO_REQUISITIONS_INTERFACE_ALL,并允许请求(名称:导入申请) SELECT ...

  6. Android内置和外置SD卡的位置获取

    public class StorageUtils { private static String TAG="123"; // 获取主存储卡路径 内置内存卡路径 public st ...

  7. Android学习笔记AutoCompleteTextView的使用

    activity_main.xml <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android&qu ...

  8. 以太坊系列之十六:golang进行智能合约开发

    以太坊系列之十六: 使用golang与智能合约进行交互 以太坊系列之十六: 使用golang与智能合约进行交互 此例子的目录结构 token contract 智能合约的golang wrapper ...

  9. libcurl坑

    code = curl_easy_setopt(conn, CURLOPT_URL, ca.strUrl.c_str()); 要char* 不能string

  10. linux 改变系统时间

    date  查看系统时间 date -s 04/05/16  日期设置成2016年4月5日 date -s 15:03:32  日期设置成2016年4月5日15:03:32 上述两步可以直接写成这样一 ...