leetcode算法: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. 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
class Solution(object):
def twoSum(self, numbers, target):
"""
:type numbers: List[int]
:type target: int
:rtype: List[int]
"""
start, end = 0 , len(numbers) - 1
while True:
if numbers[start] + numbers[end] > target :
end -= 1
elif numbers[start] + numbers[end] < target :
start += 1
else :
break
return (start+1 , end+1 ) if __name__ == '__main__':
s = Solution()
res = s.twoSum([2,3,4],6)
print(res)
leetcode算法:Two Sum II - Input array is sorted的更多相关文章
- 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 ...
- [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 (两数之和之二 - 输入的是有序数组)
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 求两数相加等于一个数的位置 --------- java
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
Given an array of integers that is already sorted in ascending order, find two numbers such that the ...
- 【LeetCode】Two Sum II - Input array is sorted
[Description] Given an array of integers that is already sorted in ascending order, find two numbers ...
- 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 ...
- 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 ...
- 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 ...
- 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 ...
随机推荐
- TrueCrypt
其实很早之前就知道这个软件,不过之前没有使用,今天折腾了一把,记录下使用过程中遇见的一些问题. 百度百科: TrueCrypt,是一款免费开源的加密软件,同时支持Windows Vista,7/XP, ...
- 关于“应用程序无法启动,因为应用程序的并行配置不正确。请参阅应用程序事件日志,或使用命令行sxstrace.exe工具”问题的解决方法
今天打开QQ管家加速版的时候突然出现了这个错误,百度了下说是系统缺少Microsoft Visual C++ 20XX(运行库),下载这个安装即可解决问题.
- RedissonLock分布式锁源码分析
最近碰到的一个问题,Java代码中写了一个定时器,分布式部署的时候,多台同时执行的话就会出现重复的数据,为了避免这种情况,之前是通过在配置文件里写上可以执行这段代码的IP,代码中判断如果跟这个IP相等 ...
- PHP7变量的内部实现
PHP7变量的内部实现 受篇幅限制,这篇文章将分为两个部分.本部分会讲解PHP5和PHP7在zval结构体的差异,同时也会讨论引用的实现.第二部分会深入探究一些数据类型如string和对象的实现. P ...
- VMware安装Centos6.4及CentOS的基本设置
1.vmware安装vmware tools实现与本地磁盘文件夹的共享 2.设置Centos网络 vmware采用nat方式.虚拟机网络使用虚拟网卡(VMware Network Adapter VM ...
- Docker(四):Docker 三剑客之 Docker Compose
前两篇文章我们介绍了 Dockerfile 的使用Docker(二):Dockerfile 使用介绍,我们知道使用一个 Dockerfile 模板文件可以定义一个单独的应用容器,如果需要定义多个容器就 ...
- js获取input file文件二进制码
<html> <body> <img id="image"src=""/> <br/> <input ty ...
- Android一个小巧的记录app(便签或者日记 随心)
入驻博客园两个月今天第一次发随笔,,话不多说,直接上图展示效果 主界面用的RecyclerView的瀑布流(StaggeredGridLayoutManager),同时加上Floatbutton悬浮按 ...
- 利用CSS3制作网页动画
如何在网页中实现动画效果动态图片 flashjavascriptcss3变形是一些效果的集合如平移 旋转 缩放 倾斜效果每个效果都可以称为变形(transfrom) 它们可以分别操控元素发生平移.旋转 ...
- 基于 HTML5 Canvas 实现的文字动画特效
前言 文字是网页中最基本的元素,一般我们在网页上都是展示的静态文字,但是就效果来说,还是比较枯燥的.文字淡入淡出的动画效果在项目中非常实用,如果有某些关键的文字,可以通过这种动态的效果来提醒用户阅读. ...