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 ...
随机推荐
- Scala高手实战****第18课:Scala偏函数、异常、Lazy值编码实战及Spark源码鉴赏
本篇文章主要讲述Scala函数式编程之偏函数,异常,及Lazy 第一部分:偏函数 偏函数:当函数有多个参数,而在使用该函数时不想提供所有参数(比如函数有3个参数),只提供0~2个参数,此时得到的函数便 ...
- Spring使用ComponentScan扫描Maven多模块工程的其它模块
说明:在新建好了Maven多模块工程后,如果想要在其它模块也能使用Spring的对象管理,比如@Autowrited这些注入方式,那么就必须开启包扫描的功能才能使其进行注入到Spring的对象管理中. ...
- C#分析URL参数获取参数和值得对应列表(二)
不错博客: [C#HttpHelper]官方产品发布与源码下载---苏飞版http://www.sufeinet.com/thread-3-1-1.html http://blog.csdn.net/ ...
- delphi模态窗口跑到后面的解决办法
Delphi(68) procedure TForm1.ShowForm2;begin Self.Enabled := False; try with TForm2.Create(ni ...
- 微信 JS SDK 的 chooseImage 接口在部分安卓机上容易造成页面刷新
该问题的症状是,当调用 chooseImage 接口并选择拍照,选择照片确定之后,然后从相机返回后,当前web页面就刷新了一次,导致拍照的图片无法选择上传:但是如果直接从相册中选择图片,则不会出现这个 ...
- ARP协议具体解释之ARP动态与静态条目的生命周期
ARP协议详细解释之ARP动态与静态条目的生命周期 ARP动态条目的生命周期 动态条目随时间推移自己主动加入和删除. q 每一个动态ARP缓存条目默认的生命周期是两分钟.当超过两分钟,该条目会被删掉 ...
- 解决Spark集群无法停止
执行stop-all.sh时,出现报错:no org.apache.spark.deploy.master.Master to stop,no org.apache.spark.deploy.work ...
- iOS小技巧 - 如何生成范围随机数
生成[0, N-1]的随机数 NSUInteger r = arc4random_uniform(N); 生成[1, N]的随机数 NSUInteger r = arc4random_uniform( ...
- 用C++实现文件压缩(1 哈弗曼编码)
今天下午想把文件压缩写一下,因为我觉得这个还是比较锻炼技术的,对数据结构的要求应该比较高,权当练习了吧. 我采用的压缩方式是Huffman编码,不过比较囧的是,我拼写拼错了,我拼的是haffman,在 ...
- MyEclipse图表工具Birt的使用技巧(三)--连接webservice数据源
Web Services 技术是一套标准.它定义了应用程序怎样在Web上实现互操作. 用户能够使用不论什么语言.在不同的平台下编写Web Services.然后通过Web Services 的标准来对 ...