[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 ...
随机推荐
- mysqli扩展库的 预处理技术 mysqli stmt
问题的提出? 现在需要向mysql数据库添加100个用户,请问如何实现? 思路: 使用for循环100次,向数据库中添加100个用户. 使用批量添加 $sql1=”insert xxx”; $ssql ...
- phpcms在自定义模块中的自定义标签分页
如果你是一个经验丰富的phpcms二次开发人员,本篇文章可以忽略不计,因为这里的写法自己都觉得很恶心 今天在开发一个网站自建了一个模块叫做论坛模块,目录名称:luntan ...
- [LeetCode]题解(python):004-Median of Two Sorted Arrays
题目来源: https://leetcode.com/problems/median-of-two-sorted-arrays/ 题意分析: 这道题目是输入两个已经排好序的数组(长度为m,n),将这两 ...
- liunx下NetworkManager导致网卡不能启动
前几天在客户现场,配置一台系统为redhat 6.0的服务器,这台服务器是IBM x3755,系统是预装的.在把服务器的IP地址配置完成后,使用命令不能启动网卡.提示:弹出界面 eht0:错误:激活链 ...
- 修改TOMCAT服务器图标为应用LOGO
在tomcat下部署应用程序,运行后,发现在地址栏中会显示tomcat的小猫咪图标.有时候,我们自己不想显示这个图标,想换成自己定义的的图标,那么按如下方法操作即可: 参考网上的解决方案:1.将$TO ...
- VS2015 开发人员命令提示,如何实现记事本编程
开始,选择VS2015 开发人员命令提示,打开 找到.c文件的位置,复制位置 在VS2015 开发人员命令提示, 输入cd 位置 回车 然后输入cl 文件名 回车 这样进行编译
- leetCode 31.Next Permutation (下一个字典序排序) 解题思路和方法
Next Permutation Implement next permutation, which rearranges numbers into the lexicographically ne ...
- C#数据绑定中,时间为空时显示时间为原始日期问题
这个问题一般出现在用实体时,实体的时间格式没有加?号.加了之后就默认为空,没有数据时不会为原始时间了.
- DropDownList四级联动
前台代码: <%@ Page Language="C#" AutoEventWireup="true" CodeFile="iframe_dro ...
- java.lang.ArithmeticException: / by zero