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 ...
随机推荐
- 【前端单元测试入门03】Sinon
前端测试存在的问题 在讲Sinon之前,我们得先讲一下在学习了Mocha.chai以及enzyme之后,我们的前端测试还存在的一些问题. 比如前台测试需要与后台交互,获取后台数据后再根据相应数据进行测 ...
- java中equals方法和hashcode方法的区别和联系,以及为什么要重写这两个方法,不重写会怎样
一.在Object类中的定义为:public native int hashCode();是一个本地方法,返回的对象的地址值.但是,同样的思路,在String等封装类中对此方法进行了重写.方法调用得到 ...
- Python3 多线程编程(thread、threading模块)
threading是对thread的封装. 1.开启线程: t=threading.Thread(target=sayhi,args=('hh',)) t.start() 或者先建一个Thread的继 ...
- 关于embed的一些使用兼容
因公司需求,要做一个扫描语音播报的功能,所以用到一些音频/视频标签 考虑到 <embed> 标签对于ie的兼容性更好一些所以,我在这采用了 <embed> 标签 ...
- struts2和spring mvc的区别
在项目中使用struts2和spring mvc为了实现后台的业务代码和前台数据之间的传递,现在基本上不会有用struts2的了,几次面试问的最多的关于struts2的问题就是struts2和spri ...
- 插入排序—直接插入排序(Straight Insertion Sort)
基本思想: 将一个记录插入到已排序好的有序表中,从而得到一个新,记录数增1的有序表.即:先将序列的第1个记录看成是一个有序的子序列,然后从第2个记录逐个进行插插入到已入,直至整个序列有序为止. 要点: ...
- V-bind详细使用
v-bind 主要用于属性绑定,Vue官方提供了一个简写方式 :bind,例如: <!-- 完整语法 --> <a v-bind:href="url">& ...
- 【MySQL】MySQL零碎积累
MySQL零碎积累 ■ 在给MySQL添加新用户时可以这么操作: create user 'newUser' identified by 'password'; grant all privilege ...
- Android学习笔记1——开发环境配置
一.JDK配置 Android是基于Java进行开发的,首先需要在电脑上配置JDK(Java Development Kit).在http://www.androiddevtools.cn/下载对应系 ...
- JAVA连接SAP
1.首先需要在SAP事务码SE37中新建一个可以被远程调用的RFC 事务码:SE37 新建一个函数组:输入事务码SE37回车后,来到函数构建器屏幕,到上面一排菜单栏:转到 -> 函数组 -> ...