[LeetCode][Python]Tow Sum
# -*- coding: utf8 -*-
'''
https://oj.leetcode.com/problems/two-sum/ 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 ===Comments by Dabay===
这道题描述中没有讲明对时间复杂度的要求。
用一个while循环嵌套另外一个while循环,时间复杂度为O(NlgN)。
这里可以用空间来换时间,用O(N)空间来存储每个数的位置,这样就可以用O(N)的时间来找到答案。
因为hash表的命中时间是O(1)。 但是,先遍历一次num,存储所有的数字到hash表中;再遍历一次来查找结果,还是会超时。 考虑其实不需要存储所有的数字到hash表中,因为只要在已经存在的hash表中有我们需要的答案就可以啦。
所以一次遍历的时候,查找是否有我们需要的答案,如果没有添加这个数字到hash表中。 此题答案要求的index是我们数组index+1的,所以返回的时候各加1。
''' class Solution:
# @return a tuple, (index1, index2)
def twoSum(self, num, target):
dict = {}
for i in xrange(len(num)):
expected = target - num[i]
if expected in dict.keys():
return (dict[expected]+1, i+1)
dict[num[i]] = i def main():
s = Solution()
nums = [-3,4,3,90]
print s.twoSum(nums, 0) if __name__ == "__main__":
import time
start = time.clock()
main()
print "%s sec" % (time.clock() - start)
[LeetCode][Python]Tow Sum的更多相关文章
- 【leetcode❤python】Sum Of Two Number
#-*- coding: UTF-8 -*- #既然不能使用加法和减法,那么就用位操作.下面以计算5+4的例子说明如何用位操作实现加法:#1. 用二进制表示两个加数,a=5=0101,b=4=0100 ...
- 【leetcode❤python】 Sum of Left Leaves
#-*- coding: UTF-8 -*- # Definition for a binary tree node.# class TreeNode(object):# def __init ...
- 【LeetCode】129. Sum Root to Leaf Numbers 解题报告(Python)
[LeetCode]129. Sum Root to Leaf Numbers 解题报告(Python) 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/pr ...
- [LeetCode] 167. Two Sum II - Input array is sorted 两数和 II - 输入是有序的数组
Given an array of integers that is already sorted in ascending order, find two numbers such that the ...
- Leetcode Python Solution(continue update)
leetcode python solution 1. two sum (easy) Given an array of integers, return indices of the two num ...
- [LeetCode] 1. Two Sum 两数和
Given an array of integers, return indices of the two numbers such that they add up to a specific ta ...
- [LeetCode] 40. Combination Sum II 组合之和 II
Given a collection of candidate numbers (candidates) and a target number (target), find all unique c ...
- [LeetCode] 112. Path Sum 路径和
Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all ...
- [LeetCode] 113. Path Sum II 路径和 II
Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given su ...
随机推荐
- Android 开源控件系列_2
FileBrowserView 一个强大的文件选择控件.界面比较漂亮,使用也很简单.特点:可以自定义UI:支持复制.剪切.删除.移动文件:可以用在Fragment.ativity.DialogFrag ...
- _OBJC_CLASS_$_ errors 错误解决办法
步骤如下图: 1. 点击 Manage Schemes 2. Shared打对勾即可
- Delphi SysErrorMessage 函数和系统错误信息表
在看 API 文档时, 我们经常见到 GetLastError; 它可以返回操作后系统给的提示. 但 GetLastError 返回的只是一个信息代码, 如何返回对应的具体信息呢? FormatMes ...
- delphi 实现微信开发
大体思路: 1.用户向服务号发消息,(这里可以是个菜单项,也可以是一个关键词,如:注册会员.) 2.kbmmw web server收到消息,生成一个图文消息给微信,在图文消息中做好自己的url,在u ...
- linux md5 加密字符串和文件方法
linux md5 加密字符串和文件方法 MD5算法常常被用来验证网络文件传输的完整性,防止文件被人篡改.MD5全称是报文摘要算法(Message-Digest Algorithm 5),此算法对任意 ...
- linux ssh 不用密码自动登录的几种方法
1. 自动ssh/scp方法== A为本地主机(即用于控制其他主机的机器) ;B为远程主机(即被控制的机器Server), 假如ip为192.168.60.110;A和B的系统都是Linux 在A上运 ...
- android 常用调用系统功能
1.从google搜索内容 Intent intent = new Intent(); intent.setAction(Intent.ACTION_WEB_SEARCH); intent.putEx ...
- IOS 8弃用api
IOS 8弃用api 下面api是弃用: 的 UIApplication 方法和属性注冊通知. 使用新的API. 的 uiviewcontroller 面向接口的方法和属性. 中描写叙述的特征和大小类 ...
- Java基础笔记-面向对象2
构造函数:(当类中没有定义构造函数时,系统会默认给该类加入一个空参数的构造函数) 格式: class Person { Person(){} //空参数 } 1.函数名与类名相同, 2,不定义返回值类 ...
- Spring事务异常回滚,捕获异常不抛出就不会回滚(转载) 解决了我一年前的问题
最近遇到了事务不回滚的情况,我还考虑说JPA的事务有bug? 我想多了....... 为了打印清楚日志,很多方法我都加tyr catch,在catch中打印日志.但是这边情况来了,当这个方法异常 ...