[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 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的更多相关文章
- 167. Two Sum II - Input array is sorted - LeetCode
Question 167. Two Sum II - Input array is sorted Solution 题目大意:和Two Sum一样,这里给出的数组是有序的 思路:target - nu ...
- 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 ...
- 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 ...
- 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 ...
- 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 ...
- [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 ...
- 【LeetCode】167. Two Sum II - Input array is sorted 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 Java解法 Python解法 日期 题目地址:ht ...
- 【LeetCode】167. Two Sum II - Input array is sorted
Difficulty:easy More:[目录]LeetCode Java实现 Description Given an array of integers that is already sor ...
- 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 ...
随机推荐
- [LeetCode] 96. Unique Binary Search Trees(给定一个数字n,有多少个唯一二叉搜索树) ☆☆☆
[Leetcode] Unique binary search trees 唯一二叉搜索树 Unique Binary Search Trees leetcode java 描述 Given n, h ...
- ssl tls 证书链 根证书和叶证书查询
你基本上需要做的是构建一个证书链,如果你没有得到它作为一个链.证书链基本上由第零个位置的最终实体证书(也是叶证书,链中最重要的证书)组成,其次是次要证书. CA证书是最不重要的. 所以这是通常的X.5 ...
- Ubuntu16.10下使用VSCode开发.netcore
按照通常的套路,首先创建一个空白的解决方案,需要用到.netcore sdk命令: dotnet new sln -o dotnetcore_tutrorial 这个时候可以看到在目标目录下生成了一个 ...
- windows 常用dos命令
explorer目录 打开当前目录 explorer . 打开上级目录 explorer .. 打开任意目录 explorer dirname cls 命令 清屏屏幕,屏幕显示的所有字符信息都是存放在 ...
- centos6.5 安装PHP7.0支持nginx
1.安装PHP所需要的扩展 yum -y install libxml2 libxml2-devel openssl openssl-devel bzip2 bzip2-devel ...
- unity3D打包发布Apk详细步骤
1.复制android-sdk-windows文件夹到C盘或者D盘或者你可以找到的任意盘任意目录,注意:不能在中文目录下!! 复制完成之后,打开unity,新建一个项目,打开Edit-Preferen ...
- python2和python3的区别总结
python2x和python3x区别: python2x:源码重复,不规范. python3x: 源码规范,优美,清晰,简单. 编译型:将代码一次性全部转化成字节码. 代表语言:C,C++ 优点: ...
- day27-python阶段性复习-基础
一.基础资料,安装python Python 跨平台的,(Linux,Windows,mac) 网站www.python.org 解释器交互方式 Ipython Python shell https: ...
- websphere部署war包
通过websphere部署以及打包成war的web项目. (1)安装配置war包,部署项目 登录websphere,进入websphere主页(依次选中) Applications --> ...
- (C/C++学习笔记) 八. 程序控制语句
八. 程序控制语句 ● 基础知识 算法的基本控制结构: 顺序结构(sequential structure), 选择结构(case structure), 循环结构(loop structure) c ...