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.
class Solution(object):
def twoSum(self, numbers, target):
"""
:type numbers: List[int]
:type target: int
:rtype: List[int]
"""
n=len(numbers)
start=0
end=n-1
while end>start:
c=numbers[start]+numbers[end]
if c==target:
return start+1,end+1
elif c>target:
end-=1
elif c<target:
start+=1

  

[LeetCode&Python] Problem 167. Two Sum II - Input array is sorted的更多相关文章

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

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

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

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

  4. 167. Two Sum II - Input array is sorted@python

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

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

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

  7. 【LeetCode】167. Two Sum II - Input array is sorted 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 Java解法 Python解法 日期 题目地址:ht ...

  8. 【LeetCode】167. Two Sum II - Input array is sorted

    Difficulty:easy  More:[目录]LeetCode Java实现 Description Given an array of integers that is already sor ...

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

随机推荐

  1. CentOS中/英文环境切换教程(CentOS6.8)

    一.前言 对于不习惯英文的人可能想将系统由英文转成中文:而对于考虑客户端如果没正确配置,中文目录可能显示为乱码的人则可能宁愿将系统由中文转成英文. 中文切换为英文,实际就是将LANG的值由zh_CN- ...

  2. summary_16th Nov, 2018

    一. 编程语言的分类: a. 机器语言:直接使用二进制指令去编写程序,必须考虑硬件细节 b:汇编语言:用英文标签取代二进制指令去编写程序,必须考虑硬件细节 c:高级语言:用人类能理解的方式编写程序,通 ...

  3. shell 流程控制语句

    case语句 case $变量名 in "值1")   如果变量的值等于值1,则执行程序1 ;;  "值2")   如果变量的值等于值2,则执行程序2 ;;   ...

  4. SQLServer中sql for xml path 的用法

    我们通常需要获取一个多行的某个字段拼出的字符串,我们可以使用for xml path进行处理:下面将介绍for xml path的具体用法: 创建测试表&插入测试数据 在数据库中新增测试表 C ...

  5. Spring框架基本代码

    1.准备阶段: 2.基本引入: 接口: package com.xk.spring.kp01_hello; public interface IHello { public void nice(); ...

  6. 【资料收集】Converting Between cv::Mat and QImage or QPixmap

    参考: 方法一 Convert between cv::Mat and QImage 两种图片类转换 - Grandyang - 博客园 http://www.cnblogs.com/grandyan ...

  7. Hibernate基础知识

    Hibernate Hibernate的作用: 1.         Hibernate解决ORM(对象关系映射)的问题,大大减少了持久层的代码量 2.         hql方言,解决了可移植性问题 ...

  8. Toy Factory

    Factory is a design pattern in common usage. Please implement a ToyFactory which can generate proper ...

  9. Cracking The Coding Interview 4.7_暂存

    //原文: // // You have two very large binary trees: T1, with millions of nodes, and T2, with hundreds ...

  10. 7.5 C++基本序列式容器

    参考:http://www.weixueyuan.net/view/6402.html 总结: vector可以理解为可以在两端插入.删除数据的数组,它提供了丰富的成员函数,用于操作数据. begin ...