leetcode 【 Two Sum 】python 实现
题目:
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 实现的更多相关文章
- [leetcode]Path Sum @ Python
原题地址:https://oj.leetcode.com/problems/path-sum/ 题意: Given a binary tree and a sum, determine if the ...
- leetcode Combination Sum python
Given a set of candidate numbers (C) and a target number (T), find all unique combinations in C wher ...
- leetcode two sum python
class Solution: # @param {integer[]} nums # @param {integer} target # @return {integer[]} def twoSum ...
- 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 ...
- 剑指offer 65. 不用加减乘除做加法(Leetcode 371. Sum of Two Integers)
剑指offer 65. 不用加减乘除做加法(Leetcode 371. Sum of Two Integers) https://leetcode.com/problems/sum-of-two-in ...
- [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 ...
- [LeetCode]题解(python):112 Path Sum
题目来源 https://leetcode.com/problems/path-sum/ Given a binary tree and a sum, determine if the tree ha ...
- [LeetCode]题解(python):064-Minimum Path Sum
题目来源 https://leetcode.com/problems/minimum-path-sum/ Given a m x n grid filled with non-negative num ...
- [LeetCode]题解(python):124-Binary Tree Maximum Path Sum
题目来源: https://leetcode.com/problems/binary-tree-maximum-path-sum/ 题意分析: 给定一棵树,找出一个数值最大的路径,起点可以是任意节点或 ...
- [LeetCode]题解(python):040-Combination Sum II
题目来源 https://leetcode.com/problems/combination-sum-ii/ Given a collection of candidate numbers (C) a ...
随机推荐
- bank conflct 一句话总结
由于最新的多播模式区别于原来的广播模式,原来同一个warp不同线程访问同一个bank的相同地址不再是bank conflict, 现在总结为:只要同一个 warp 的不同线程会访问到同一个 bank ...
- vue中使用setTimeout
在vue的函数中使用setTimeout self.distroyTimeout = setTimeout(()=>{ self.initData() },1000) 这时清除setTimeou ...
- #linux 下Sublime的安装
1.Download http://www.sublimetext.com/2 Installtion use tar 解压压缩包,这里我将包改了个名字,这样就不用写空格的转义字符了,改成Subli ...
- 一些好的IOS blog 不断增加中。。。。
http://www.swiftkiller.com/?p=371 http://blog.csdn.net/javayujiafeng/article/details/14163319 http:/ ...
- hdu-1874 畅通工程续---模板题
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1874 题目大意: 求起点到终点的最短距离 解题思路: 注意重边,其他的就是模板 #include&l ...
- bzoj1189 [HNOI2007]紧急疏散
Description 发生了火警,所有人员需要紧急疏散!假设每个房间是一个N M的矩形区域.每个格子如果是'.',那么表示这是一块空地:如果是'X',那么表示这是一面墙,如果是'D',那么表示这是一 ...
- 【BZOJ1101】[POI2007] Zap(莫比乌斯反演)
点此看题面 大致题意: 求\(\sum_{x=1}^N\sum_{y=1}^M[gcd(x,y)==d]\). 一道类似的题目 推荐先去做一下这道题:[洛谷2257]YY的GCD,来初步了解一下莫比乌 ...
- 【BZOJ4300】绝世好题(位运算水题)
点此看题面 大致题意: 给你一个序列\(a\),让你求出最长的一个子序列\(b\)满足\(b_i\&b_{i-1}!=0\). 位运算+\(DP\) 考虑设\(f_i\)表示以第\(i\)个数 ...
- 【BZOJ3925】[ZJOI2015] 地震后的幻想乡(状压期望DP)
点此看题面 大致题意: 有\(n\)个点和\(m\)条边,每条边的权值是一个\(0\sim1\)的随机实数,要你用\(n-1\)条边将图联通,问这\(n-1\)条边中边权最大值的期望最小值. 提示 这 ...
- 【转】CentOS 7.0 安装Redis 3.2.1详细过程和使用常见问题
http://www.linuxidc.com/Linux/2016-09/135071.htm 环境:CentOS 7.0 Redis 3.2.1 Redis的安装与启动 这里我把Redis放在/h ...