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. 解题
1 class Solution: 2 def twoSum(self, numbers: List[int], target: int) -> List[int]: 3 i,j=0,len(numbers)-1 4 while(i<j): 5 if numbers[i]+numbers[j]==target: 6 return[i+1,j+1] 7 elif numbers[i]+numbers[j]>target: 8 j-=1 9 else: 10 i+=1
转载于:https://www.cnblogs.com/king-lps/p/10799366.html
167. Two Sum II - Input array is sorted两数之和相关推荐
- 167. Two Sum II - Input array is sorted 两数之和 II - 输入有序数组
Title 给定一个已按照升序排列 的有序数组,找到两个数使得它们相加之和等于目标数. 函数应该返回这两个下标值 index1 和 index2,其中 index1 必须小于 index2. 说明: ...
- 167. Two Sum II - Input array is sorted (C, C++, Python)
本文讲述了Array类中第167个问题的几种解法,实现语言包括C,Python以及C++. 问题: Given an array of integers that is already sorted ...
- 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 tha ...
- leetcode python3 简单题167. Two Sum II - Input array is sorted
1.编辑器 我使用的是win10+vscode+leetcode+python3 环境配置参见我的博客: 链接 2.第一百六十七题 (1)题目 英文: Given an array of intege ...
- [LeetCode By Python]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 ...
- 【LeetCode 剑指offer刷题】数组题2:57 有序数组中和为s的两个数(167 Two Sum II - Input array is sorted)...
[LeetCode & 剑指offer 刷题笔记]目录(持续更新中...) 57 有序数组中和为s的两个数 题目描述 输入一个递增排序的数组和一个数字S,在数组中查找两个数,是的他们的和正好是 ...
- leetcode 167 Two Sum II - Input array is sorted
给定一个有序的数组,和一个整数目标,求两个数的和等于目标的索引,索引从1开始.假设必定存在解. 有两种思路: 直接找: vector<int> twoSum(vector<int&g ...
- 167. Two Sum II - Input array is sorted
问题描述 解决方案 class Solution { public:vector<int> twoSum(vector<int>& numbers, int targe ...
- LeetCode 167. Two Sum II - Input array is sorted--Python解法
题目地址:Two Sum II - Input array is sorted - LeetCode Given an array of integers that is already sorted ...
最新文章
- 【php】php对mysql的连接操作【mysql】
- 使用Mockito时遇到的一些问题
- Effective C++学习第二天
- BadgerDAO锁仓量超过9亿美元
- php 获取当前action,ThinkPHP3.2.2获取当前Action名称
- [mysql终极优化]之主从复制与读写分离详细设置教程
- 一秒学会,小白也能上手,最简单的装逼代码
- 音视频开发系列(10):基于qt的音频推流
- 在线查看计算机配置,怎样查看电脑配置信息?
- CSP201912-3化学方程式
- input range: vue自定义进度条
- Bootstrap4使用教程
- confluence7安全补丁_Confluence 7 伴随程序的安装
- PHP的ES入门(一)—— Index的创建和删除
- 支付宝 android 指纹支付,指纹支付教程放出!支付宝支持指纹支付!
- android设备外接键盘实现监听事件
- Centos7下Mysql 安装及简单配置
- 请允许ActiveX控件和页面其他部分交互
- 面向对象的特征之一:封装性
- 使用由 Python 编写的 lxml 实现高性能 XML 解析
热门文章
- 蜘蛛搜索引擎_「SEO工具」如何利用爱站工具去分析各搜索引擎蜘蛛情况?
- c语言创造线性表储存复数,《c语言数结构》第02章 线性表.ppt
- 转换php script类型,ECMAScript 类型转换
- java生成excel中文乱码,JSP应用导出Excel报表的简单实现以及中文乱码彻底解决(HTML)...
- DE29 Matrix Exponentials
- 使用迁移学习后使用微调再次提高模型训练的准确率
- 掌握了AI这些点,面试官的天选之子就是你
- 视频讲解(RCNN,FasterRCNN,FPN,MaskRCNN)
- 基于R-Net、QA-Net和BiDAF实现中文观点型问题机器阅读理解
- java备份mysql的单个表_MySQL 使用java备份某张表的操作