题目

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

The function twoSum should return indices of the two numbers such that they add up to the target, where index1 must be less than index2. Please note that your returned answers (both index1 and index2) are not zero-based.

You may assume that each input would have exactly one solution.

Input: numbers={2, 7, 11, 15}, target=9
Output: index1=1, index2=2

代码:oj测试通过 Runtime: 54 ms

 class Solution:
# @return a tuple, (index1, index2)
def twoSum(self, num, target):
number_index={}
for i in range(len(num)):
if number_index.has_key(target-num[i]):
return (number_index[target-num[i]]+1,i+1)
number_index[num[i]]=i

思路

这个不是我想的。网上一大堆,不多说了。

leetcode 【 Two Sum 】python 实现的更多相关文章

  1. [leetcode]Path Sum @ Python

    原题地址:https://oj.leetcode.com/problems/path-sum/ 题意: Given a binary tree and a sum, determine if the ...

  2. leetcode Combination Sum python

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

  3. leetcode two sum python

    class Solution: # @param {integer[]} nums # @param {integer} target # @return {integer[]} def twoSum ...

  4. LeetCode:Path Sum I II

    LeetCode:Path Sum Given a binary tree and a sum, determine if the tree has a root-to-leaf path such ...

  5. 剑指offer 65. 不用加减乘除做加法(Leetcode 371. Sum of Two Integers)

    剑指offer 65. 不用加减乘除做加法(Leetcode 371. Sum of Two Integers) https://leetcode.com/problems/sum-of-two-in ...

  6. [LeetCode]题解(python):113 Path Sum II

    题目来源 https://leetcode.com/problems/path-sum-ii/ Given a binary tree and a sum, find all root-to-leaf ...

  7. [LeetCode]题解(python):112 Path Sum

    题目来源 https://leetcode.com/problems/path-sum/ Given a binary tree and a sum, determine if the tree ha ...

  8. [LeetCode]题解(python):064-Minimum Path Sum

    题目来源 https://leetcode.com/problems/minimum-path-sum/ Given a m x n grid filled with non-negative num ...

  9. [LeetCode]题解(python):124-Binary Tree Maximum Path Sum

    题目来源: https://leetcode.com/problems/binary-tree-maximum-path-sum/ 题意分析: 给定一棵树,找出一个数值最大的路径,起点可以是任意节点或 ...

  10. [LeetCode]题解(python):040-Combination Sum II

    题目来源 https://leetcode.com/problems/combination-sum-ii/ Given a collection of candidate numbers (C) a ...

随机推荐

  1. 跨平台移动开发phonegap/cordova 3.3全系列教程-app启动画面

    1.app启动画面设计 用photoshop设计启动画面,图片分辨率为720*1280 保存的文件名为splash.png 将splash.png复制到res\drawable,如图 PS:要先添加闪 ...

  2. python logging 模块记录日志

    #日志记录到多文件示例 import logging def error_log(message): file_1_1 = logging.FileHandler('error.log', 'a+', ...

  3. Mysql update后insert造成死锁原因分析及解决

    系统中出现死锁的日志如下: ) TRANSACTION: , ACTIVE sec inserting mysql tables , locked LOCK WAIT lock struct(s), ...

  4. Intel&amd

  5. 分析ELF的加载过程

    http://blog.chinaunix.net/uid-72446-id-2060538.html 对于可执行文件来说,段的加载位置是固定的,程序段表中如实反映了段的加载地址.对于共享库来?段的加 ...

  6. NOIP2018初赛 解题报告

    前言 \(NOIP2018\)初赛已经结束了,接下来就要准备复赛了. 不过,在此之前,还是先为初赛写一篇解题报告吧. 单项选择题 送分题.(虽然我还是做错了)可以考虑将它们全部转化为\(10\)进制, ...

  7. 【51nod1815】调查任务(Tarjan+拓扑)

    点此看题面 大致题意:有\(N\)个城市由\(M\)条单向道路(图不一定联通),每个城市有一个发达程度\(a[i]\),要求你求出首都\(S\)到城市\(i\)的一条路径上的两个不同城市\(x,y\) ...

  8. python_34_文件操作3

    f=open('yesterday',encoding='utf-8') print(f.tell())#文件句柄所在指针指向的位置,即光标在哪里(按字符计数) f.readline()#读一行 pr ...

  9. js字符串的使用

    Javascript的内置功能之一就是字符串连接,如果+号用于两个字符串连接 var s="hello,world"       //想要查找给定位置的字符       s.cha ...

  10. BeyondCompare:如何之比较文件内容的不同?

    问题描述: 在使用beyond compare比较文件的时候,常会有很多不同,但是点击打开后,发现内容没有不同.这个是因为工具把文件的日期.大小等非内容因素也比较了进去. 解决方法: 点击“会话” - ...