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. 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> res;
map<int, int> exist,index;
for(int i=0; i<numbers.size(); i++){
exist[numbers[i]]++;
index[numbers[i]]=i;
}
for(int i=0; i<numbers.size(); i++)
if(target-numbers[i]==numbers[i]&&exist[numbers[i]]>=2){
res.push_back(i+1);
res.push_back(i+2);
break;
}
else if(exist[target-numbers[i]]==1&&numbers[i]*2!=target){
res.push_back(i+1);
res.push_back(index[target-numbers[i]]+1);
break;
}
return res;
}
};
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
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 ...
- 167. Two Sum II - Input array is sorted - LeetCode
Question 167. Two Sum II - Input array is sorted Solution 题目大意:和Two Sum一样,这里给出的数组是有序的 思路:target - nu ...
随机推荐
- 安装使用electron辛路历程
安装使用electron辛路历程 成功安装electron以及成功使用第一个应用,整整花费了我一整天的时间,各种百度,各种尝试.最终,终于总结了一个亲测可行的终极可执行方案: electron 简单介 ...
- 在myeclipse中maven遇见的问题
An internal error occurred during: "Retrieving archetypes:". Java heap space 表示你的myeclipse ...
- Lightoj 1090 - Trailing Zeroes (II)
题目连接: http://www.lightoj.com/volume_showproblem.php?problem=1090 题目大意: 给出n,r,p,q四个数字1<=n,r,p,q< ...
- 题解报告:hdu1994利息计算
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1994 Problem Description 为自行解决学费,chx勤工俭学收入10000元以1年定期 ...
- Matlab实现图像分割 分类: 图像处理 2014-06-14 21:31 662人阅读 评论(1) 收藏
下面使用极小值点阈值选取方法,编写MATLAB程序实现图像分割的功能. 极小值点阈值选取法即从原图像的直方图的包络线中选取出极小值点, 并以极小值点为阈值将图像转为二值图像 clear all; cl ...
- UIAlertController的使用,代替UIAlertView和UIActionSheet
在iOS8以后,UIAlertView就开始被抛弃了. 取而代之是UIAlertController 以前是警示框这样写: UIAlertView *alert = [[UIAlertView all ...
- 171 Excel Sheet Column Number Excel表列序号 26进制转10进制
给定一个Excel表格中的列名称,返回其相应的列序号.示例: A -> 1 B -> 2 C -> 3 ... Z -> 26 AA -&g ...
- javaScript关闭浏览器 (不弹出提示框)
一段JavaScript脚本程序,负责关闭窗口,如果网页不是通过脚本程序打开的(window.open()),调用window.close()脚本关闭窗口前,必须先将window.opener对象置为 ...
- 防止系统页面被加载进 iframe 子窗口
在controller的返回的响应头中添加 response.addHeader("x-frame-options", "DENY"); 即可
- mysql 忘记密码 登陆+修改密码
step1: 苹果->系统偏好设置->最下边点mysql 在弹出页面中 关闭mysql服务(点击stop mysql server) step2: 进入终端输入:cd /usr/local ...