Leetcode每日一题:167.two-sum-ii-input-array-is-sorted(两数之和Ⅱ-输入有序数组)
解题思路:双指针 l 和 r,分别指向头和尾,对于这个有序数组,如果number[l] + number[r] 比 target大
,那么说明选择的r对应元素偏大,将指针 r 左移;如果小于target ,那么相应的将指针 l 右移,直到两者相等!
vector<int> twoSum(vector<int> &numbers, int target)
{vector<int> res;int len = numbers.size();if (len == 0)return res;int l = 0, r = len - 1;while (l < r){if (numbers[l] + numbers[r] == target){res.push_back(l + 1);res.push_back(r + 1);break;}if (numbers[l] + numbers[r] > target){r--;continue;}if (numbers[l] + numbers[r] < target){l++;continue;}}return res;
}
Leetcode每日一题: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两数之和
1. 原始题目 给定一个已按照升序排列 的有序数组,找到两个数使得它们相加之和等于目标数. 函数应该返回这两个下标值 index1 和 index2,其中 index1 必须小于 index2. 说明 ...
- leetcode python3 简单题167. Two Sum II - Input array is sorted
1.编辑器 我使用的是win10+vscode+leetcode+python3 环境配置参见我的博客: 链接 2.第一百六十七题 (1)题目 英文: Given an array of intege ...
- 【LeetCode 剑指offer刷题】数组题2:57 有序数组中和为s的两个数(167 Two Sum II - Input array is sorted)...
[LeetCode & 剑指offer 刷题笔记]目录(持续更新中...) 57 有序数组中和为s的两个数 题目描述 输入一个递增排序的数组和一个数字S,在数组中查找两个数,是的他们的和正好是 ...
- 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
给定一个有序的数组,和一个整数目标,求两个数的和等于目标的索引,索引从1开始.假设必定存在解. 有两种思路: 直接找: vector<int> twoSum(vector<int&g ...
- 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 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 ...
- 167. Two Sum II - Input array is sorted
问题描述 解决方案 class Solution { public:vector<int> twoSum(vector<int>& numbers, int targe ...
最新文章
- 20145129 课程总结
- java web项目目录报错_netdevgirl.通过maven创建javaweb项目
- phpmailer 发送邮件空隙太大_WordPress纯代码无插件开启SMTP邮件服务——墨涩网
- Go语言编程:Go语言实现快速排序算法
- 【codevs30521022】多米诺覆盖,二分图
- 《Python从小白到大牛》第5章 Python编码规范
- mysql8.0依赖_分享MySql8.0.19 安装采坑记录
- 记录数据库内一条记录的更新时间
- 江苏高考成绩什么时候可以查询2021,2021年江苏高考成绩什么时候公布出来,几月几号几点钟可以查询...
- HTML+JS调用摄像头拍照并上传图片
- 电音插件auto_自动电音基调查询软件助手完美支持32_64bit系统所有电音插件
- 【知识分享】汽车搭载的车载摄像头分类
- Rosalind第16题——ros_bio16_MPRT
- 如何在word里插入矢量图
- 震旦复印机扫描到服务器文件夹,办公室复印机怎么扫描文件(图示复印机扫描功能应用)...
- python音乐编程_可以编程写音乐的python库musicpy教程(第一期) musicpy的数据结构...
- 报错:Unhandled exception
- 单个正态总体均值的区间估计_总体均值的区间估计 (正态总体: σ2 已知实例).pdf...
- php经常致命错误怎么办,如何捕获PHP中的致命错误(Fatal Errors)
- 电脑版android版iphone版ipad版,[下载]微软发布iPad版Office iPhone版Android版彻底免费...