167. Two Sum II - Input array is sorted (二分ortwo-pointer)
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) {
int n = numbers.size();
vector<int>w();
for (int i = ; i < n; ++i) {
int x = target - numbers[i];
int p = lower_bound(numbers.begin() + i + ,numbers.end(),x) - numbers.begin();
if (p < n && (numbers[i] + numbers[p] == target)) {
w[] = i + ;
w[] = p + ;
break;
}
}
return w;
}
};
另一种解法更巧妙 two-pointer
class Solution {
public:
vector<int> twoSum(vector<int>& numbers, int target) {
int lo=, hi=numbers.size()-;
while (numbers[lo]+numbers[hi]!=target){
if (numbers[lo]+numbers[hi]<target){
lo++;
} else {
hi--;
}
}
return vector<int>({lo+,hi+});
}
};
167. Two Sum II - Input array is sorted (二分ortwo-pointer)的更多相关文章
- 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【easy】
167. Two Sum II - Input array is sorted[easy] Given an array of integers that is already sorted in a ...
- 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 两数和 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 ...
- 167. Two Sum II - Input array is sorted (Array)
Given an array of integers that is already sorted in ascending order, find two numbers such that the ...
随机推荐
- CF1029E Tree with Small Distances
题目描述 给定一棵树.要求往树中加入一些边使得从1到其他节点的距离至多是2 . 输出加入边的最小数量.(边全部都是无向的) 题解:好多人都说是贪心,但是我写的是树形dp. (这道题实在太像小胖守皇宫了 ...
- [LUOGU] P3354 [IOI2005]Riv 河流
题目描述 几乎整个Byteland王国都被森林和河流所覆盖.小点的河汇聚到一起,形成了稍大点的河.就这样,所有的河水都汇聚并流进了一条大河,最后这条大河流进了大海.这条大河的入海口处有一个村庄--名叫 ...
- galera cluster安装与配置
由于公司数据量与并发的日渐增大,普通的主从复制已无法满足要求.对比了网上PXC.galera.mysql cluster等方案,最终决定选择galera cluster. 以下为安装步骤: 1.下载g ...
- 五段实用的js高级技巧
技巧一之setTimeout. 应用案例:比如你想一个函数循环执行10次,怎么办?以前通常是先setInterval,然后clearInterval,技巧一就是克服这个问题 复制代码 代码如下: (f ...
- UVA 12100 打印队列(STL deque)
题意: 给定n个优先级打印队列,然后从0开始编号到n-1.出队一个元素,如果他是队列中优先级最高的,打印(耗时一分钟),否则放到队尾(不耗时).给定一个m,求位置m的文件打印的时间. 分析: 用一个p ...
- 怎样判断有没有SQL注入?
最为经典的单引号判断法: 在参数后面加上单引号,比如: http://xxx/abc.php?id=1' 如果页面返回错误,则存在 Sql 注入. 原因是无论字符型还是整型都会因为单引号个数不匹配而报 ...
- jQuery_DOM学习之------包裹元素的方法
1..wrap( ):在集合中匹配的每个元素周围包裹一个HTML结构 简单的看一段代码: <span>连接文字</span> 给span元素增加一个a包裹 $('span'). ...
- [luoguP1981] 表达式求值(U•ェ•*U)
传送门 弄两个栈,一个存数,一个存运算符,然后乱搞. 代码 #include <cstdio> #include <cstring> #include <iostream ...
- bzoj 1005 [HNOI2008] 明明的烦恼 (prufer编码)
[HNOI2008]明明的烦恼 Time Limit: 1 Sec Memory Limit: 162 MBSubmit: 5907 Solved: 2305[Submit][Status][Di ...
- ssh远程登录
ssh root@192.168.124.128 密钥登录: 1).ssh-keygen 生成公钥和私钥 [root@rhel5 ~]# ssh-keygen -t rsa Generating pu ...