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. Zabbix监控系统

    前言: 一个初略自动化运维平台,应该实现以下3个层面自动化: 1.操作系统层面自动化 如果想要万台服务器共舞,没有操作系统这个舞台还怎么舞? 1.1:物理环境: OS预备自动安装(Pxe/KickSt ...

  2. 【转】Vue中mintui的field实现blur和focus事件

    首先上代码说总结: <mt-field label="卡号" v-model="card.cardNo" @blur.native.capture=&qu ...

  3. makefile文件写法解析

    一.makefile文件示例 makefile文件并不难写,一个makefile模版如下所示,所有makefile文件在此基上稍微修改就可以了. # this is a makefile #这一行是注 ...

  4. weblogic开启http访问日志并实时写入日志文件

    由于http访问会产生大量日志,耗去不少IO和CPU所以在生产一般是不启用的:但有时我们会想启用http访问日志,尤其是在系统上线调试的时候. weblogic的日志默认在domain_name/se ...

  5. Chrome使用的plugin

    Chrome使用的plugin   翻译 google翻译 youlict划词翻译     书签   查询书签 neater bookmarks 博客园收藏网页 书签保存 bookmark sysnc ...

  6. matlab 调试日志

    debug=; diary off if debug delete('log.txt'); !del log.txt diary('log.txt'); diary ON end diary OFF

  7. Cyclic Components CodeForces - 977E(DFS)

    Cyclic Components CodeForces - 977E You are given an undirected graph consisting of nn vertices and  ...

  8. Win10系列:JavaScript 数据绑定

    使用数据绑定可以使页面中元素的属性值与数据源中的数据同步,其中数据源可以来自数据库.文件以及自定义的数据等.在常用的数据绑定方法中,简单对象绑定是将HTML元素与一个仅包含数据的简单对象相绑定,模板绑 ...

  9. 遍历所有子物体中renderer(渲染器)中的material(材质)并改变其alpha值实现若隐若现的效果

    using UnityEngine;using System.Collections;using UnityEngine.UI; public class CubeControl : MonoBeha ...

  10. [BZOJ1588]营业额统计

    Problem 每次给你一个数,找出前面的数与这个数的差的绝对值的最小值 Solution Splay Notice 找不到前驱和后继时,会出错. Code #include<cmath> ...