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 ...
随机推荐
- Azure VMSS ---- PowerShell创建标准镜像的VMSS集群
VMSS的创建可以采用Portal.Powershell.Azure CLI或者Template. 但目前Portal创建有很多限制,本文将介绍如何用PowerShell来创建VMSS的集群. 具体的 ...
- Qt多选框
1.获取并显示复选框文本内容 ui->label->setText(ui->comboBox->currentText());
- MySQL 常用启动,关闭,登录脚本
MySQL 常用启动,关闭,登录脚本 规范化mysql的启动,关闭,登录 1 cat mysql_env.ini #set env MYSQL_USER=system #注意用户权限 MYSQL_PA ...
- JavaScript基本概念B - 关于方法
方法也是对象 这个事需要反复强调.方法是 类型 Function 的对象,和其他对象一样,它也有方法. function gen() { return function ans(factor) { r ...
- vesamenu.c32:not a COM32R image报错解决方案
今天用U盘刻录安装Linux Mint 的时候,一直出现 vesamenu.c32:not a COM32R image这个报错,查了很久,原因好像是电脑是老电脑的原因.处理的办法很简单,只需要输入l ...
- 用JS,做一个简单的计算器
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title&g ...
- Web Api2 中线程的使用
System.Threading.Thread th = new System.Threading.Thread(方法名); th.IsBackground = true; th.Start(); 上 ...
- maven ...../.m2/settings.xml
<?xml version="1.0" encoding="UTF-8"?> <settings xmlns="http://mav ...
- import time
时间相关的操作,时间有三种表示方式: 时间戳 1970年1月1日之后的秒,即:time.time() 格式化的字符串 2014-11-11 11:11, 即:t ...
- 关于stickybroadcast
stickybroadcast顾名思义,粘性广播,从字面上我们可以联想到service的返回值中也有个一stick,在service中stick作用是当返回了之后服务被杀死,会重启服务. 但是这里的s ...