leetcode 167 two sum II
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. 双指针问题,由于是排过序的列表,可以左指针指向起始,右指针指向末尾。两个指针根据条件收缩。最终找到符合的两个值。
如果numbers[left] + numbers[right] 大于target,则右指针左移,使和减小。
如果numbers[left] + numbers[right] 小于target,则左指针右移,使和增大。
class Solution(object):
def twoSum(self, numbers, target):
"""
:type numbers: List[int]
:type target: int
:rtype: List[int]
"""
left = 0
right = len(numbers) - 1
while left < right:
add = numbers[left] + numbers[right]
if add == target:
return [left + 1, right + 1]
elif add < target:
left = left + 1
else:
right = right - 1
leetcode 官网有效率更高的代码,感兴趣的可以搜下。
leetcode 167 two sum II的更多相关文章
- 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 : 数组/二分查找/双指针
一. 题目 1. Two Sum II Given an array of integers that is already sorted in ascending order, find two n ...
- [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
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
Problem: Given an array of integers that is already sorted in ascending order, find two numbers such ...
- ✡ 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 ...
- 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 ...
随机推荐
- uva-10282-枚举
题意:语言翻译, 直接map即可 #include "pch.h" #include <string> #include<iostream> #includ ...
- python学习笔记_week10
一.多进程multiprocessing io 操作不占用cpu,计算占cpu(如1+1),上下文切换耗资源(多线程可能不如单线程快),python多线程不适合cup密集操作型的任务,适合io操作密集 ...
- python中的expandtabs、\t
expandtabs()将tab转换成空格,默认1个tab转成8个空格,\t制表符代表一个tab,我们也可以自定义转换成几个空格 举个例子: 1 a = "hello\tworld" ...
- python中的extend
extend()拓展列表,批量写入 举个例子: 1 a = ["hello", "world", "dlrb"] 2 b = [1, 2, ...
- JUC 之 ThreadPoolExecutor 的一些研究
ThreadPoolExecutor 概述:===================================================================== 构造函数: 4个 ...
- Dom对象和jQuery对象的相互转化
01.jQuery对象 1.jQuery对象就是通过对jQuery包装dom对象后产生的对象. 2.虽然jQuery对象是包装DOM对象后产生的,但是jQuery无法使用DOM对象的任何方法,同理DO ...
- PHP 时间相关操作
使用函式 date() 实现 <?php echo $showtime=date("Y-m-d H:i:s");?> 显示的格式: 年-月-日 小时:分钟:秒 获得当天 ...
- ISO7816之管脚定义
卡座的管脚定义 如果使用示波器或者逻辑分析仪来观察 连接C3.C5.C7 小技巧当C3为3.57MHZ时候,可以使用波特率为9600的串口来监听.
- 17_react脚手架应用分析
|-- index.html // 启动页(主页) |-- build //构建目录,遵循发布系统规范 | |-- index.html //静态页面 | |-- static //资源文件发布到cd ...
- mui init 出现无法引入子页面问题
1. 检查项目中是否重复出现了 mui.init() 函数; mui.init({ subpages: [{ styles: { // top: "44px", top: &quo ...