167. Two Sum II - Input array is sorted两数之和
1. 原始题目
给定一个已按照升序排列 的有序数组,找到两个数使得它们相加之和等于目标数。
函数应该返回这两个下标值 index1 和 index2,其中 index1 必须小于 index2。
说明:
- 返回的下标值(index1 和 index2)不是从零开始的。
- 你可以假设每个输入只对应唯一的答案,而且你不可以重复使用相同的元素。
示例:
输入: numbers = [2, 7, 11, 15], target = 9
输出: [1,2]
解释: 2 与 7 之和等于目标数 9 。因此 index1 = 1, index2 = 2 。
2. 思路
双指针法。左右指针分别往中间移,如果两数之和等于target则返回索引,否则如果两数之和大于target,则右端应该往左移来减小两数之和。否则左端指针往右移来增大1两数之和。
3. 解题
class Solution:
def twoSum(self, numbers: List[int], target: int) -> List[int]:
i,j=0,len(numbers)-1
while(i<j):
if numbers[i]+numbers[j]==target:
return[i+1,j+1]
elif numbers[i]+numbers[j]>target:
j-=1
else:
i+=1
167. Two Sum II - Input array is sorted两数之和的更多相关文章
- 167 Two Sum II - Input array is sorted 两数之和 II - 输入有序数组
给定一个已按照升序排列 的有序数组,找到两个数使得它们相加之和等于目标数.函数应该返回这两个下标值 index1 和 index2,其中 index1 必须小于 index2.请注意,返回的下标值(i ...
- [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] 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 ...
- 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 - LeetCode
Question 167. Two Sum II - Input array is sorted Solution 题目大意:和Two Sum一样,这里给出的数组是有序的 思路:target - nu ...
- 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
Problem: Given an array of integers that is already sorted in ascending order, find two numbers such ...
随机推荐
- MQTT协议通俗讲解
参考 Reference v3.1.1 英文原版 中文翻译版 其他资源 网站 MQTT官方主页 Eclipse Paho 项目主页 测试工具 MQTT Spy(基于JDK) Chrome插件 MQTT ...
- GWT实现“跟随标题栏”
在一些商城或者博客上,随处可见一些“跟随的标题栏”,什么是”跟随的标题栏“呢?它的效果是当浏览器窗口移到看不见某处内容的时候,这块内容的标题会紧贴在浏览器顶部,跟随浏览器滑动,当用户将浏览器内容往上拖 ...
- LINUX必须记住的指令
写在前面: 1,<你一定要知道的关于Linux文件目录操作的12个常用命令>是楼主收集的关于Linux文件目录操作最常用的命令,包括文件或目录的新建.拷贝.移动.删除.查看等,是开发人员操 ...
- 【转】onclick事件与href='javascript:function()'的区别
href='javascript:function()'和onclick能起到同样的效果,一般来说,如果要调用脚本还是在onclick事件里面写代码,而不推荐在href='javascript:fun ...
- 实验吧CTF题库-密码学(部分)
这里没有key: 打开链接,有一个弹窗 然后就是一个空白网页,右键查看源代码 这里有一串js密文,解密一下,https://www.dheart.net/decode/index.php 得到flag ...
- java selenium webdriver第二讲 页面元素定位
自动化测试实施过程中,测试程序中常用的页面操作有三个步骤 1.定位网页上的页面元素,并存储到一个变量中 2.对变量中存储的页面元素进行操作,单击,下拉或者输入文字等 3.设定页面元素的操作值,比如,选 ...
- C语言学习笔记--函数与指针
1. 函数类型 (1)C 语言中的函数有自己特定的类型,这个类型由返回值.参数类型和参数个数共同决定.如 int add(int i,int j)的类型为 int(int,int). (2)C 语言中 ...
- DAY11-MYSQL多表查询
一 介绍 本节主题 多表连接查询 复合条件连接查询 子查询 准备表 #建表 create table department( id int, name ) ); create table employ ...
- Template Method模式和Strategy模式有何异同
Template Method模式和Strategy模式有何异同 博客分类: 设计模式 Java Template Method模式很容易理解,就是由基类提供一个模板,将各子类中不变的行为提取到基类 ...
- mt_rand()函数、str_shuffle() 函数、join() 函数
mt_rand() 使用 Mersenne Twister 算法返回随机整数. 语法 mt_rand(min,max) 定义和用法 str_shuffle() 函数随机地打乱字符串中的所有字符. 语法 ...