(双指针 二分) 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 that they add up to a specific target number.
The function twoSum should return indices of the two numbers such that they add up to the target, where index1 must be less than index2.
Note:
- Your returned answers (both index1 and index2) are not zero-based.
- You may assume that each input would have exactly one solution and you may not use the same element twice.
Example:
Input: numbers = [2,7,11,15], target = 9
Output: [1,2]
Explanation: The sum of 2 and 7 is 9. Therefore index1 = 1, index2 = 2.
-----------------------------------------------------------------------------------------------------------------
1)
由于数组是已经排序好的,所以用二分查找,时间复杂度为O(nlogn)。就是先遍历数组,然后查找这个数的右边的是否有一个数,这个数与它相加得到目标数。
C++代码:
class Solution {
public:
vector<int> twoSum(vector<int>& numbers, int target) {
for(int i = ;i < numbers.size(); i++){
int t = target - numbers[i],left = i + ,right = numbers.size() - ;
while(left <= right){
int mid = left + (right - left)/;
if(numbers[mid] == t) return {i+,mid+};
else if(numbers[mid] < t) left = mid + ;
else right = mid - ;
}
}
return {};
}
};
2)
不过二分查找的时间复杂度比较大,可以用双指针,时间复杂度为线性。空间复杂度为O(1)。
C++代码:
class Solution {
public:
vector<int> twoSum(vector<int>& numbers, int target) {
int l = ,r = numbers.size() - ;
while(l < r){
if(numbers[l] + numbers[r] == target) return {l+,r+};
else if(numbers[l] + numbers[r] > target) r--;
else l++;
}
return {};
}
};
(双指针 二分) leetcode 167. Two Sum II - Input array is sorted的更多相关文章
- 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 ...
- [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 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 the ...
- 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 ...
- ✡ leetcode 167. Two Sum II - Input array is sorted 求两数相加等于一个数的位置 --------- java
Given an array of integers that is already sorted in ascending order, find two numbers such that the ...
- Java [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 th ...
- LeetCode - 167. Two Sum II - Input array is sorted - O(n) - ( C++ ) - 解题报告
1.题目大意 Given an array of integers that is already sorted in ascending order, find two numbers such t ...
- 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 that the ...
- 167. Two Sum II - Input array is sorted - LeetCode
Question 167. Two Sum II - Input array is sorted Solution 题目大意:和Two Sum一样,这里给出的数组是有序的 思路:target - nu ...
随机推荐
- CRM实施失败?请注意这6大问题及对策!
据Gartner调查显示:约50%的CRM项目不能达到客户期望.这一点与很多其他的IT项目状况非常类似,大多出于管理问题,供应商服务能力,项目执行不善等. 另据一个在线CRM论坛调查其失败的原因:67 ...
- SVN的使用说明
SVN管理版本是比较好的东西,但是我的SVN图标老是不出来,有时候莫名其妙就没有了,所以记录一下处理办法: 1.右键->TortoiseSVN->Settings->Icon Ove ...
- docker根据配置文件启动redis
更多docker基本命令请自行查询. 1.首先拉取合适版本的docker镜像 docker pull redis:5 2.如果不需要更改什么配置或者仅仅测试用可以直接启动镜像运行容器,这里要说明的是根 ...
- easyui实现分页
主要参考官方的文档,欢迎评论 1.集成easyui,下面是我的引入方式,我引入到了head.html 每次只要引入该页面就可以了. <!-- easyui样式支持 --><link ...
- 光盘安装win7系统教程
光盘安装系统是最传统的安装系统的方法,虽然现在U盘安装和硬盘安装已经很方便,但仍有很多用户习惯光盘安装的方式,下面小编教大家如何利用光盘安装系统. 来源:https://www.haoxitongx. ...
- 安利一下workflowy和Dynalist
最近有比较幕布这款笔记记录软件,很是喜欢那种可以记录笔记的方式,不过我还追溯了一下这种笔记模式,发现了workflowy和Dynalist这两款风格一样的软件.大家可以自行注册试试.其中Dynalis ...
- 什么是 CI/CD?
什么是 CI/CD? 在软件开发中经常会提到持续集成Continuous Integration(CI)和持续交付Continuous Delivery(CD)这几个术语.但它们真正的意思是什么呢? ...
- Core官方DI解析(3)-ServiceCallSite.md
上一篇说过在整个DI框架中IServiceProviderEngine是核心,但是如果直接看IServiceProviderEngine派生类其实看不出也没什么东西,因为这个类型其实都是调用的其它对象 ...
- pyqt5在textBrowser添加文本并自动滑动到底
pyqt5在textBrowser添加文本并自动滑动到底 说明: 1.按下按钮pushButton,把单行文本框lineEdit里的内容循环不断的添加到多行文本展示框textBrowser.2.必须要 ...
- linq中如何在join中指定多个条件
public ActionResult Edit(int id) { using (DataContext db = new DataContext(ConfigurationManager.Conn ...