【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 ...
随机推荐
- grails框架的g:paginate分页标签的使用
我用到的grails是2.4.4. 该版本下游一个标签g:paginate 该标签下有以下几个参数:total(必须要填写的项).controller.action.prev.max.offset等等 ...
- 运维请注意:”非常危险“的Linux命令大全
Linux命令是一种很有趣且有用的东西,但在你不知道会带来什么后果的时候,它又会显得非常危险.所以,在输入某些命令前,请多多检查再敲回车. rm –rf rm –rf是删除文件夹和里面附带内容的一种最 ...
- XPath函数——字符串函数(转载)
本文是转载的,原文网址:http://www.cnblogs.com/zhaozhan/archive/2010/01/17/1650242.html 字符串函数主要用来处理字符串.字符串函数主要包括 ...
- AE开发中栅格图层实现分级渲染
GP工具IDW执行后,生成的栅格图层是黑白二色,需要手动进行分级渲染,似乎不是所有栅格图层都可以进行分级渲染,注意异常处理.注意ARCMAP中是有颜色的,无需自己处理. IRasterClassify ...
- C语言基础(7)-float,double,long double类型
1.定义方式 3.14这个就是一个浮点常量,3f是一个浮点类型的常量 float a;//定义了一个浮点类型的小数变量,名字叫a double b;//定义了一个double类型的变量,名字叫b lo ...
- 高效的jQuery
选择捷径 // 糟糕 if(collection.length > 0){..} // 建议 if(collection.length){..} 熟记技巧 // 糟糕 $('#id').data ...
- javascript数据结构与算法---列表
javascript数据结构与算法---列表 前言:在日常生活中,人们经常要使用列表,比如我们有时候要去购物时,为了购物时东西要买全,我们可以在去之前,列下要买的东西,这就要用的列表了,或者我们小时候 ...
- Swift 3.0 【Swift 3.0 相较于 Swift 2.2 的变化】
一.编译器和语法变化 函数或方法参数 调用函数或方法时从第一个参数开始就必须指定参数名 在Swift的历史版本中出现过在调用函数时不需要指定任何函数参数(或者从第二个参数开始指定参数名),在调用方法时 ...
- ASP.NET Session 详解
阅读本文章之前的准备 阅读本文章前,需要读者对以下知识有所了解.否则,阅读过程中会在相应的内容上遇到不同程度的问题. 懂得ASP/ASP.NET编程 了解ASP/ASP.NET的Session模型 了 ...
- 守护进程demon.c
1 #include <stdio.h> #include <unistd.h> #include <errno.h> #include <time.h> ...