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

    1.优化JS逻辑,在上传前先同步相同文件进度,提高多用户上传效率. 2.优化文件块保存逻辑,减少相同文件块的写入操作,减少服务器IO操作,提高上传效率.   js变化: up6.js新增UrlQuer ...

  2. iOS play video

    iOS: How to use MPMoviePlayerController up vote6down votefavorite 3 I've created a blank project (iO ...

  3. (转)Expression 表达式树学习整理

    原文地址:http://www.cnblogs.com/li-peng/p/3154381.html 整理了一下表达式树的一些东西,入门足够了 先从ConstantExpression 开始一步一步的 ...

  4. 解决在cmder中bash(WSL)上下箭头不能使用问题

    有三种解决方式,第一种方式最简单实用 安装新版本wslbridge 这个解决方法最简单,最实用,下载第三方wslbridge,安装即可使用. 这时再进入cmder,运行bash.exe,可以发现上下左 ...

  5. Atcoder Grand Contest 031B(DP,思维)

    #include<bits/stdc++.h>using namespace std;int a[200007];int b[200007];long long dp[200007];lo ...

  6. List_Delete

    /*Sorting from little to large use List*/ #include <stdio.h> /* printf, scanf, NULL */ #includ ...

  7. kali linux之sqlmap

    一款开源的命令行自动SQL注入工具,它能够对多种主流数据库进行扫描支持,基于Python环境. 检测动态页面中get/post参数,cookie,http头 数据榨取/文件系统访问 操作系统命令执行 ...

  8. django中ModelForm解决多表单组合显示问题

    一.多表单组合显示问题 在项目中用ModelForm生成页面时 当有多表单组合显示时,会显示全部的关联表单数据. 而在实际项目中可能会出现只想让用户选择部分数据,这时候这样的显示就有问题. 二.问题解 ...

  9. Java面向对象之关键字final 入门实例

    一.基础概念 1.关键字final可以修饰类.函数.变量. 2.关键字final修饰的类不可以被继承. 3.关键字final修饰的方法不可以被覆盖. 4.关键字final修饰的变量是一个常量,只能被赋 ...

  10. json_decode转换数组过程中,结果为null处理办法,百分之百有效

    json_decode这个函数是json_encode的反函数,一般传递数据的时候为了压缩数据,会将数组格式的数据转换成json格式,用到的函数就是json_encode,然后接收到数据之后再用jso ...