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 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. Please note that 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.
Input: numbers={2, 7, 11, 15}, target=9
Output: index1=1, index2=2
解法一:
class Solution {
public:
vector<int> twoSum(vector<int>& numbers, int target) {
vector<int> result; for (int i = , j = numbers.size() -; i <= j;) {
if (numbers[i] + numbers[j] == target) {
result.push_back(i + );
result.push_back(j + );
return result;
}
else if (numbers[i] + numbers[j] > target) {
--j;
}
else if (numbers[i] + numbers[j] < target) {
++i;
}
} return result;
}
};
双指针
解法二:
class Solution {
public:
vector<int> twoSum(vector<int>& numbers, int target) {
vector<int> result; for (int i = ; i < numbers.size() - ; ++i) {
int start = i + ;
int end = numbers.size() - ;
int temp_target = target - numbers[i]; //binary search
while (start + < end) {
int mid = start + (end - start) / ;
if (numbers[mid] == temp_target) {
result.push_back(i + );
result.push_back(mid + );
return result;
}
else if (numbers[mid] > temp_target) {
end = mid;
}
else if (numbers[mid] < temp_target) {
start = mid;
}
}
if (numbers[start] == temp_target) {
result.push_back(i + );
result.push_back(start + );
return result;
}
if (numbers[end] == temp_target) {
result.push_back(i + );
result.push_back(end + );
return result;
}
} return result;
}
};
二分查找
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 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 - 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
Difficulty:easy More:[目录]LeetCode Java实现 Description Given an array of integers that is already sor ...
- [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 ...
- 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 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 ...
随机推荐
- openresty总结
协程 1.例如当获取的数据没有前后依赖关系时,可以使用ngx.thread.spawn和ngx.thread.wait同时从数据库不同的库.表或者不同来源(mysql,redis等)获取数据. htt ...
- Linux查看系统开机时间(转)
1.who命令查看 who -b查看最后一次系统启动的时间. who -r查看当前系统运行时间 2.last reboot last reboot可以看到Linux系统历史启动的时间. 重启一下操作 ...
- [Bug]CS0016: 未能写入输出文件“c:\Windows\Microsoft.NET\Framework\v4.0.30319\Temp
win7中安装asp.net的问题 编译器错误信息: CS0016: 未能写入输出文件问题解决办法 编译错误 说明: 在编译向该请求提供服务所需资源的过程中出现错误.请检查下列特定错误详细信息并适当地 ...
- JavaScript中的模块化之AMD和CMD
前言: 为什么我们需要模块化开发,模块化开发的好处有哪些? 首先我们先说一下非模块化的开发方式带来的弊端. 非模块化开发中会导致一些问题的出现,变量和函数命名可能相同,会造成变量污染和冲突,并且出错时 ...
- ODATA4 及实现
ODATA4 的JAVASCRIPT 实现: http://jaydata.org/ ODATA4 的JAVA 项目 Apache Olingo:http://olingo.incubato ...
- 异常值监测的方法 Tukey test
参考: https://www.zhihu.com/question/38066650
- 如何看待Linux操作系统的用户空间和内核空间
作为中央核心处理单元的CPU,除了生产工艺的不断革新进步外,在处理数据和响应速度方面也需要有权衡.稍有微机原理基础的人都知道Intel X86体系的CPU提供了四种特权模式ring0~ring3,其中 ...
- How to rebuild RPM database on a Red Hat Enterprise Linux system?
本文是笔者最近遇到的一个故障的处理过程,解决方案是Rebuild RPM 的DB,后面内容其实是REDHAT官方的solutions,不过我遇到的现象和解决方案都与官方有点出入,故一直帖出来: 我遇到 ...
- 在hadoop作业中自定义分区和归约
当遇到有特殊的业务需求时,需要对hadoop的作业进行分区处理 那么我们可以通过自定义的分区类来实现 还是通过单词计数的例子,JMapper和JReducer的代码不变,只是在JSubmit中改变了设 ...
- knowledgeroot 配置
首先下载 KnowledgeRoot 的安装包,就是一个压缩文件,解压缩后放到 WebRoot 下面 在浏览器中打开网站,自动提示进行安装,安装的过程很简单,安装结束后即可以使用. 安装包创建的数据库 ...