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 ...
随机推荐
- HDU2579(bfs迷宫)
Dating with girls(2) Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Oth ...
- springboot+springcloud config
参考:sorry,全找不到了,当时没记录,最后后知后觉觉得应该记录,所以后面的都有在asfood父项目中的doc文件夹下记录,望见谅. 1. springconfig server 1.1. pom. ...
- java流类基础练习。
总结:BufferedReader.InputStreamReader.字节与字符的区别. package com.ds; import java.io.*; //从控制台输入字符串,输到sdd时停止 ...
- Redis事务和watch
redis的事务 严格意义来讲,redis的事务和我们理解的传统数据库(如mysql)的事务是不一样的. redis中的事务定义 Redis中的事务(transaction)是一组命令的集合. 事务同 ...
- Redis 集群之 Redis-Cluster
Redis集群官方推荐方案 Redis-Cluster 集群 redis cluster 通过分片实࣫容量扩展 通过主从复制实࣫节点的高可用 节点之间互相通信 每个节点都维护整个集群的节点信息 red ...
- linux下面的挂载点讲解
linux.unix这类操作系统将系统中的一切都作为文件来管理.在windows中我们常见的硬件设备.磁盘分区等,在linux.unix中都被视作文件,对设备.分区的访问就是读写对应的文件.挂载点实际 ...
- c# pictureBox 循环播放图片
c# 1.遍历目录 查找图片 2.在 pictureBox 循环播放 public void PlayThread()//CMD_UpdateBtnStatus cmd { Int32 framera ...
- python学习笔记(一):python简介和入门
最近重新开始学习python,之前也自学过一段时间python,对python还算有点了解,本次重新认识python,也算当写一个小小的教程.一.什么是python?python是一种面向对象.解释型 ...
- Oracle 10g RAC全库flashback
因业务原因,今天需要做一次全库flashback.以下是操作全过程: 1.确认主库是否能flashback到需要的时间点 在节点1上执行: SQL> alter session set nls_ ...
- 图解JVM和Tomcat类加载机制
说到本篇的tomcat类加载机制,不得不说翻译学习tomcat的初衷. 之前实习的时候学习javaMelody的源码,但是它是一个Maven的项目,与我们自己的web项目整合后无法直接断点调试.后来同 ...