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 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. 双指针问题,由于是排过序的列表,可以左指针指向起始,右指针指向末尾。两个指针根据条件收缩。最终找到符合的两个值。
如果numbers[left] + numbers[right] 大于target,则右指针左移,使和减小。
如果numbers[left] + numbers[right] 小于target,则左指针右移,使和增大。
class Solution(object):
def twoSum(self, numbers, target):
"""
:type numbers: List[int]
:type target: int
:rtype: List[int]
"""
left = 0
right = len(numbers) - 1
while left < right:
add = numbers[left] + numbers[right]
if add == target:
return [left + 1, right + 1]
elif add < target:
left = left + 1
else:
right = right - 1

leetcode 官网有效率更高的代码,感兴趣的可以搜下。


leetcode 167 two sum II的更多相关文章

  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. [LeetCode] #167# Two Sum II : 数组/二分查找/双指针

    一. 题目 1. Two Sum II Given an array of integers that is already sorted in ascending order, find two n ...

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

  4. 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 ...

  5. (双指针 二分) 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 ...

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

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

  7. ✡ leetcode 167. Two Sum II - Input array is sorted 求两数相加等于一个数的位置 --------- java

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

  8. Java [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 th ...

  9. LeetCode - 167. Two Sum II - Input array is sorted - O(n) - ( C++ ) - 解题报告

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

随机推荐

  1. 《算法》第二章部分程序 part 1

    ▶ 书中第二章部分程序,加上自己补充的代码,包括插入排序,选择排序,Shell 排序 ● 插入排序 package package01; import java.util.Comparator; im ...

  2. Docker 初学

    据我理解, 它最大的用途是 将我们的应用及环境整个打包, 这样如果我们的开发环境环境部署了,就不用再分别去测试/ 生产环境部署了! -- 但是, 新问题在于, 拷贝这些东西比较麻烦... Docker ...

  3. Shell 编程(实例二)

    创建一个脚本,为指定硬盘创建分区 1.列出当前系统所有磁盘,让用户选择,如果选择quit则退出脚本:如果选择错误,则重新选择 2.当用户选择完成后,询问是否继续 3.抹除选择磁盘上的分区,为其创建三个 ...

  4. 详述 IntelliJ IDEA 插件的安装及使用方法

    首先,进入插件安装界面: Mac:IntelliJ IDEA -> Preferences -> Plugins; Windows:File -> Settings -> Pl ...

  5. Choose unique values for the 'webAppRootKey' context-param in your web.xml files!

    在Tomcat的server.xml中配置两个context,出现其中一个不能正常启动,交换配置顺序,另一个又不能正常启动,即始终只有第二个配置能启动的情况.如果单独部署,都没有问题.报错大致内容如下 ...

  6. iOS 内存管理,ARC

    iOS 对象内存释放时机:当对象的引用计数为0时对象被释放. 所以如下代码: __weak NSObject * a = [[NSObject alloc] init]; 这个对象在创建完赋完值后会被 ...

  7. linux 常用命令记录&& xsheel 使用记录

    cp -r x1 x2                     复制文件x1到x2 ls                                   当前目录下的文件列表 ll         ...

  8. guava collection/cache初探

    写了上面一篇,看了点eventbus相关的guava代码后,发现里面用到了很多其他guava包里的方法,所以顺着看一下,比如之前用到的map都是guava自己的 Multimap:可以包含有几个重复K ...

  9. eclipse 使用Maven deploy命令部署构建到Nexus

    转载于:http://blog.csdn.net/jun55xiu/article/details/43051627 1  应用场景:SYS-UTIL(系统工具)项目部署.构建成JAR包(SYS-UT ...

  10. 使用ansible安装docker以及docker-compose

    转自:https://www.cnblogs.com/jsonhc/p/7879028.html 环境三台centos7主机: master:192.168.101.14,node1:192.168. ...