【leetcode】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
解题思想:
首先我很是不甘心的直接暴力了一次,果断TLE。然后想到先排序,然后两个指针前后搜,大了就后面的指针前移,小了就前面的指针后移。不过比较麻烦的是要找出原来的位置,导致代码多了几个丑陋的循环。
#coding=utf-8
class Solution:
# @return a tuple, (index1, index2)
def twoSum(self, num, target):
t = []
for i in num:
t.append(i)
num.sort()
l = len(num)
index1 = 0
index2 = l-1
while index1 < index2:
if num[index1] + num[index2] == target:
break
elif num[index1] + num[index2] > target:
index2 -= 1
elif num[index1] + num[index2] < target:
index1 += 1
for i in range(l):
if num[index1] == t[i]:
index1 = i
break
for i in range(l):
if num[index2] == t[i] and i != index1:
index2 = i
break
if index1 >index2:
return index2+1,index1+1
return index1+1,index2+1
不过后来看到的标准解法里用了哈希表,We could reduce the runtime complexity of looking up a value to O(1) using a hash map that maps a value to its index
。于是就用dict写了下,不过可能是python的dict比较慢还是怎么,速度几乎一样,不过代码倒是短了不少。
class Solution:
# @return a tuple, (index1, index2)
def twoSum(self, num, target):
t = {}
l = len(num)
for i in range(l):
t[num[i]] = i
for i in range(l):
if t.has_key(target-num[i]):
if i != t[target-num[i]]:
return i+1 , t[target-num[i]]+1
【leetcode】Two Sum的更多相关文章
- 【LeetCode】129. Sum Root to Leaf Numbers 解题报告(Python)
[LeetCode]129. Sum Root to Leaf Numbers 解题报告(Python) 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/pr ...
- 【leetcode】907. Sum of Subarray Minimums
题目如下: 解题思路:我的想法对于数组中任意一个元素,找出其左右两边最近的小于自己的元素.例如[1,3,2,4,5,1],元素2左边比自己小的元素是1,那么大于自己的区间就是[3],右边的区间就是[4 ...
- 【LeetCode】633. Sum of Square Numbers
Difficulty: Easy More:[目录]LeetCode Java实现 Description https://leetcode.com/problems/sum-of-square-n ...
- 【Leetcode】404. Sum of Left Leaves
404. Sum of Left Leaves [题目]中文版 英文版 /** * Definition for a binary tree node. * struct TreeNode { * ...
- 【LeetCode】Two Sum II - Input array is sorted
[Description] Given an array of integers that is already sorted in ascending order, find two numbers ...
- 【leetcode】Path Sum IV
If the depth of a tree is smaller than 5, then this tree can be represented by a list of three-digit ...
- 【LeetCode】Path Sum(路径总和)
这道题是LeetCode里的第112道题.是我在学数据结构——二叉树的时候碰见的题.题目要求: 给定一个二叉树和一个目标和,判断该树中是否存在根节点到叶子节点的路径,这条路径上所有节点值相加等于目标和 ...
- 【LeetCode】Maximize Sum Of Array After K Negations(K 次取反后最大化的数组和)
这道题是LeetCode里的第1005道题. 题目描述: 给定一个整数数组 A,我们只能用以下方法修改该数组:我们选择某个个索引 i 并将 A[i] 替换为 -A[i],然后总共重复这个过程 K 次. ...
- 【LeetCode】404. Sum of Left Leaves 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目大意 题目大意 解题方法 递归 迭代 日期 [LeetCode] 题目地址:h ...
- 【LeetCode】1022. Sum of Root To Leaf Binary Numbers 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 DFS 日期 题目地址:https://leetco ...
随机推荐
- JAVA锁的膨胀过程和优化
首先说一下锁的优化策略. 1,自旋锁 自选锁其实就是在拿锁时发现已经有线程拿了锁,自己如果去拿会阻塞自己,这个时候会选择进行一次忙循环尝试.也就是不停循环看是否能等到上个线程自己释放锁.这个问题是基于 ...
- Tarjan三把刀
搞过OI的对tarjan这个人大概都不陌生.这个人发明了很多神奇的算法,在OI届广被采用. 他最广泛采用的三个算法都是和$dfn$,$low$相关的. 有向图求强连通分量 其实说直白点,就是缩点.用得 ...
- centos6.5升级python为2.7
今天线上服务器全部升级python环境为python-2.7.6的环境,我采用的方法是ansible+shell,代码如下,友提,Python-2.7.6.tgz.setuptools-14.3.1. ...
- 3Struts2进阶----青软S2SH(笔记)
关于上面这个红框里的问题,经过实际测试发现,struts2增加一个命名空间后,jsp页面里所引用的资源的路径,也需要增加一个"../", 于是,跟SpringMVC没啥区别了啊.. ...
- diff和patch的使用、patch文件的格式解说
为了弄懂 patch中的 p0 p1 和.orig文件是啥,找到了这篇文章! 来源:http://www.cnblogs.com/super119/archive/2010/12/18/19 ...
- MRDS学习四——自动型机器车
由自己的所在开始,探索自己周围的简单机器车,假设车子的行走路径如下: 我们要把L型路径写成一个Activity,然后由外部输入这个L的大小,最后这个Activity要能够在完成行走路径时吐出更大的L大 ...
- JSP Servlet的区别
主要体现在两点 1.JSP是类servlet jsp说得简单点就是用另一套简单的规则写的servlet程序,它可以写java代码,还可以写html代码,JavaScript,css等等……,但是到服务 ...
- 基于iSCSI的SQL Server 2012群集测试(一)--SQL群集安装
一.测试需求介绍与准备 公司计划服务器迁移过程计划同时上线SQL Server2012,引入SQL Server2012群集提高高可用性,需要对SQL Server2012群集技术进行研究.测试,确保 ...
- BZOJ2982——combination
1.题意:求 C(n,m) % 10007 ,10007是质数咯 n和m < 2000000000 2.分析:这个东西太大了,显然不能用n!的阶乘预处理的方式搞出来,也不能用递推公式搞出来 于是 ...
- Ubuntu GNOME 16.10 Beta 1问世了!
导读 Ubuntu GNOME 16.10操作系统已经进入研发周期一段时间了,今天终于可以下载Beta 1版本进行测试了.作为Ubuntu官方flavor之一,Ubuntu GNOME团队非常努力的整 ...