167. Two Sum II - Input array is sorted**
来源:互联网 发布:智慧工地软件加盟 编辑:程序博客网 时间:2024/06/14 13:54
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.
Input: numbers={2, 7, 11, 15}, target=9
Output: index1=1, index2=2
My code:
class Solution(object): def twoSum(self, numbers, target): """ :type numbers: List[int] :type target: int :rtype: List[int] """ n = len(numbers) low = 0 high = n-1 sums=numbers[low]+numbers[high] while sums!=target: if sums >target: high -=1 else: low +=1 sums=numbers[low]+numbers[high] return low+1, high+1
0 0
- Two Sum II - Input array is sorted
- Two Sum II - Input array is sorted
- Two Sum II - Input array is sorted
- Two Sum II - Input array is sorted
- Two Sum II - Input array is sorted
- Two Sum II - Input array is sorted
- Two Sum II - Input array is sorted
- Two Sum II - Input array is sorted
- Leetcode-167.Two Sum II - Input array is sorted
- [leetcode] 167. Two Sum II - Input array is sorted
- 【leetcode】167. Two Sum II - Input array is sorted
- 【LeetCode】167. Two Sum II - Input array is sorted
- 167. Two Sum II - Input array is sorted
- 167. Two Sum II - Input array is sorted
- 167. Two Sum II - Input array is sorted
- [LeetCode OJ]167. Two Sum II - Input array is sorted
- [leetcode]167. Two Sum II - Input array is sorted
- 167. Two Sum II - Input array is sorted
- Visual Stdio C++ 编译器 编译 (GSL) GNU Scientific Library 的方法介绍(8)
- MATLAB转换TXT中灰度值数据格式,将灰度数据输出为图像
- neuraltalk2代码解析-(3)-LanguageModule.lua
- JAVA和C++比教学 :算式执行顺序
- nginx编译安装
- 167. Two Sum II - Input array is sorted**
- C++里面的const是怎么实现的?
- 详解显示意图和隐式意图
- httpd编译安装
- 传递闭包
- Android蓝牙(一)
- python3 标准类型
- android控件——ProgressDialog
- 基于内容的推荐系统