题目:

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. 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 and you may not use the same element twice.

Input: numbers={2, 7, 11, 15}, target=9
Output: index1=1, index2=2

代码:

第一想法是二分求解,冷静下来后仍然使用两个指针,左指针(0)右指针(len(nums)-1)的数相加大于target,右指针左移,小左指针右移,相等加1输出

class Solution(object):def twoSum(self, numbers, target):""":type numbers: List[int]:type target: int:rtype: List[int]"""leftnum = 0; rightnum = len(numbers)-1while leftnum<rightnum:a = numbers[leftnum]+numbers[rightnum]if a>target:rightnum-=1elif a<target:leftnum+=1else:return [leftnum+1,rightnum+1]return
a = Solution()
print a.twoSum([1,2,7,11,15,18],9)
输出:
➜  leetcode python leecoder.py
[2, 3]

[LeetCode By Python]167. Two Sum II - Input array is sorted相关推荐

  1. 167. Two Sum II - Input array is sorted (C, C++, Python)

    本文讲述了Array类中第167个问题的几种解法,实现语言包括C,Python以及C++. 问题: Given an array of integers that is already sorted ...

  2. 【LeetCode 剑指offer刷题】数组题2:57 有序数组中和为s的两个数(167 Two Sum II - Input array is sorted)...

    [LeetCode & 剑指offer 刷题笔记]目录(持续更新中...) 57 有序数组中和为s的两个数 题目描述 输入一个递增排序的数组和一个数字S,在数组中查找两个数,是的他们的和正好是 ...

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

  4. leetcode python3 简单题167. Two Sum II - Input array is sorted

    1.编辑器 我使用的是win10+vscode+leetcode+python3 环境配置参见我的博客: 链接 2.第一百六十七题 (1)题目 英文: Given an array of intege ...

  5. leetcode 167 Two Sum II - Input array is sorted

    给定一个有序的数组,和一个整数目标,求两个数的和等于目标的索引,索引从1开始.假设必定存在解. 有两种思路: 直接找: vector<int> twoSum(vector<int&g ...

  6. 167. Two Sum II - Input array is sorted 两数之和 II - 输入有序数组

    Title 给定一个已按照升序排列 的有序数组,找到两个数使得它们相加之和等于目标数. 函数应该返回这两个下标值 index1 和 index2,其中 index1 必须小于 index2. 说明: ...

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

    问题描述 解决方案 class Solution { public:vector<int> twoSum(vector<int>& numbers, int targe ...

  8. 167. Two Sum II - Input array is sorted两数之和

    1. 原始题目 给定一个已按照升序排列 的有序数组,找到两个数使得它们相加之和等于目标数. 函数应该返回这两个下标值 index1 和 index2,其中 index1 必须小于 index2. 说明 ...

  9. LeetCode 167. Two Sum II - Input array is sorted--Python解法

    题目地址:Two Sum II - Input array is sorted - LeetCode Given an array of integers that is already sorted ...

最新文章

  1. 零基础入门学习Python(32)-丰富的else语句及简洁的with语句
  2. Python中dict用法详解
  3. linux下文件系统的启动过程
  4. Linux CenOS7下安装ActivetMQ
  5. Elasticsearch Mapping 详解
  6. 网络通信协议-TCP/IP模型实战
  7. AE插件Aura Rowbyte Aura for Mac(AE几何粒子渲染效果插件)
  8. JavaScript NaN 属性
  9. MathType中公式不对齐怎么办
  10. Rejected request from RFC1918 IP to public server address
  11. kinect获取实时深度数据
  12. 同济大学c语言程序设计答案,2020年同济大学道路与铁道工程考研真题试卷及试题答案,汽车理论及设计考研试题下载...
  13. word新建文档默认文件名_如何更改保存Word文档时使用的默认文件名
  14. js设置延时加载事件
  15. java mediator_Java 实现中介者(Mediator)模式
  16. Django的CBV和FBV
  17. javascript常用知识点集
  18. LeetCode题解(面试10.11):峰与谷(Python)
  19. 服务器托管vs自建机房哪个好?
  20. PC DMIS三坐标产品测量编程视频教程+资料

热门文章

  1. 2-单级交流放大电路【电子技术】
  2. 拼多多 2019校园招聘 正式批笔试-2018.8.28
  3. Https证书校验不当引起的安全问题
  4. JVM(六)---jvm压缩指针
  5. 常见公文批示用语集锦
  6. 数据与广告系列二十四:效果广告后定向时代如何逆流而上
  7. 亚洲诚信助力看雪安全开发者峰会
  8. 拉仇恨!李想靠微博股票赚了两三倍,只有千把万
  9. 计算机二级小蒋在教务处负责学生成绩,小蒋是一位中学教师,在教务处负责初一年级学生的成绩管理。由于学校地处偏远地区,缺乏必要的教学_考题宝...
  10. Android 原生控件之一 TextView