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 ...
随机推荐
- Angular CLI的简单使用(2)
刚才创建了myApp这个项目,看一下这个项目的文件结构. 项目文件概览 Angular CLI项目是做快速试验和开发企业解决方案的基础. 你首先要看的文件是README.md. 它提供了一些如何 ...
- C#运算符、控制流
1 运算符 1.1 一元运算符: -(负号).+(正号):可以省略 1.2 二元运算符: 优先级,*(乘)./(除).%(取余).+(加).-(减).=(赋值) 二元赋值运算符,=.+=.-= ...
- Cocos2d-x v3.1 安装图文教程(二)
Cocos2d-x v3.1 安装图文教程(二) 如果我们需要在Android平台上运行就必须安装android的SDK,如果我们只想在window上运行就只需要安装Cocos2d-x就行了.当 ...
- 【51nod1299】监狱逃离(树形DP)
点此看题面 大致题意: 在一棵树中有\(N\)条边连接\(N+1\)个节点,现在已知这棵树上的\(M\)个节点,要求封住最少的节点,使这\(M\)个节点中的任意一个节点无法到达叶子节点,若能办到输出最 ...
- python_41_with语句
#为了避免打开文件后忘记关闭,可以通过管理上下文,即:with open('log','r') as f: # 如此方式,当with代码块执行完毕时,内部会自动关闭并释放文件资源. with open ...
- 阿里云服务器下安装LAMP环境(CentOS Linux 6.3)(1)
阿里的云服务器准备好以后,我们首先要做的就是把自己购买的磁盘空间挂载到系统里面,我们为服务器选择的是 Linux 系统,确切说的是 CentOS 系统. 默认阿里云服务器带了一个 20G 的空间,一般 ...
- 前端-带header和footer的双栏布局
目标是实现如上图带header和footer的双栏布局,其中右侧sidebar是固定宽度,左侧content是自适应: https://www.zybuluo.com/dengzhirong/note ...
- C++性能优化笔记
最近着手去优化项目中一个模块的性能.该模块是用C++实现,对大量文本数据进行处理. 一开始时,没什么思路,因为不知道性能瓶颈在哪里.于是借助perf工具来对程序进行分析,找出程序的性能都消耗在哪里了. ...
- Java - 网络
要事为先,你如果想要在这个行业发展下去的话,实际上三角形的三个点在支撑着你发展,一个是技术.一个是管理(不是说管理别人,是管理你自己的时间,管理你自己的精力).还有一个就是沟通,注重这三点均衡的发展. ...
- nodeJS 服务端文件上传
var http = require('http'); var path = require('path'); var fs = require('fs'); function uploadFile( ...