Given an array of integers that is already sorted in ascending order, 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.

Note:

  • Your returned answers (both index1 and index2) are not zero-based.
  • You may assume that each input would have exactly one solution and you may not use the same element twice.

Example:

Input: numbers = [2,7,11,15], target = 9
Output: [1,2]
Explanation: The sum of 2 and 7 is 9. Therefore index1 = 1, index2 = 2.

原题地址: Two Sum II - Input array is sorted

难度: Easy

思路1:

采用缓存的方式,用字典记录每一个值的索引

代码:

class Solution(object):
def twoSum(self, numbers, target):
"""
:type numbers: List[int]
:type target: int
:rtype: List[int]
"""
d = {}
for idx, num in enumerate(numbers):
if target - num in d:
idx1 = d[target-num]
return [idx1+1, idx+1]
else:
d[num] = idx

时间复杂度: O(n)

空间复杂度: O(n)

思路2:

因为数组已经排序了,可以分别在数组首尾放置一指针,逐渐向中间夹逼

代码:

class Solution(object):
def twoSum(self, numbers, target):
"""
:type numbers: List[int]
:type target: int
:rtype: List[int]
"""
i, j = 0, len(numbers)-1
while i < j:
val = numbers[i] + numbers[j]
if val == target:
return [i+1, j+1]
elif val > target:
j -= 1
else:
i += 1

时间复杂度: O(n)

空间复杂度: O(1)

167. Two Sum II - Input array is sorted@python的更多相关文章

  1. 29. leetcode 167. Two Sum II - Input array is sorted

    167. Two Sum II - Input array is sorted Given an array of integers that is already sorted in ascendi ...

  2. 167. Two Sum II - Input array is sorted【easy】

    167. Two Sum II - Input array is sorted[easy] Given an array of integers that is already sorted in a ...

  3. 167. Two Sum II - Input array is sorted - LeetCode

    Question 167. Two Sum II - Input array is sorted Solution 题目大意:和Two Sum一样,这里给出的数组是有序的 思路:target - nu ...

  4. leetcode 1.Two Sum 、167. Two Sum II - Input array is sorted 、15. 3Sum 、16. 3Sum Closest 、 18. 4Sum 、653. Two Sum IV - Input is a BST

    1.two sum 用hash来存储数值和对应的位置索引,通过target-当前值来获得需要的值,然后再hash中寻找 错误代码1: Input:[3,2,4]6Output:[0,0]Expecte ...

  5. [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 ...

  6. 167. Two Sum II - Input array is sorted

    题目: Given an array of integers that is already sorted in ascending order, find two numbers such that ...

  7. (双指针 二分) leetcode 167. Two Sum II - Input array is sorted

    Given an array of integers that is already sorted in ascending order, find two numbers such that the ...

  8. 167. Two Sum II - Input array is sorted (Array)

    Given an array of integers that is already sorted in ascending order, find two numbers such that the ...

  9. [LeetCode&Python] Problem 167. Two Sum II - Input array is sorted

    Given an array of integers that is already sorted in ascending order, find two numbers such that the ...

随机推荐

  1. LVS-DR VIP和RIP不同网段的配置方法

    http://blog.itpub.net/25723371/viewspace-1446935/

  2. (转)linux du命令

    转自 http://www.cnblogs.com/peida/archive/2012/12/10/2810755.html Linux du命令也是查看使用空间的,但是与df命令不同的是Linux ...

  3. mysql 主从 binlog

    binlog: 用来记录mysql的数据更新或者潜在更新(update xxx where id=x effect row 0);文件内容存储:/var/lib/mysql mysqlbinlog - ...

  4. [题解](区间质数筛)POJ_2689 Prime Distance

    区间筛素数:先筛出1~sqrt(R)的素数,然后对于每个询问只要用这些素数筛掉区间内的合数即可. 几个细节:1.特判和1有关的一些情况 2.每次减去L偏移量,数组只开区间大小 3.POJ无法使用万能头 ...

  5. Web之localStorage

    localStorage: 1.localStorage拓展了cookie的4K限制 2.localStorage会可以将第一次请求的数据直接存储到本地,这个相当于一个5M大小的针对于前端页面的数据库 ...

  6. windows安装redis和PHP redis扩展

    1.安装Redis (1)这里选择在github官网上下载Redis,地址:Redis下载地址 下载压缩包(如下图),并解压到本地目录,我放在D:\wamp\redis\redis-windows ( ...

  7. Hive_Hive的管理_远程服务

    远程服务启动方式 - 端口号10000 - 启动方式: #hive --service hiveserver & 以JDBC或ODBC的程序登陆到hive中操作数据时,必须选用远程服务启动方式 ...

  8. 安装dubbo的监控中心dubbo-monitor-simple

    1.下载dubbo-monitor-simple 2.修改配置指定注册中心地址 进入dubbo-monitor-simple\src\main\resources\conf目录修改 dubbo.pro ...

  9. python 多继承(新式类) 二

    在python中,要调用父类的某个方法,python2.2之前需要如下代码: class A:def __init__(self):   print "enter A"   pri ...

  10. JS的文本框验证以及form表单的提交阻止

    js: 1.只能输入数字 只能输入数字:<input type="text" onkeyup="javascript:ReNumber(this)" /& ...