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 ...
随机推荐
- [Usaco2017 Feb]Why Did the Cow Cross the Road II (Gold)
Description 上下有两个长度为n.位置对应的序列A.B, 其中数的范围均为1~n.若abs(A[i]-B[j])<= 4,则A[i]与B[j]间可以连一条边. 现要求在边与边不相交的情 ...
- HttpURLConnection教程
1.Class Overview An URLConnection for HTTP (RFC 2616) used to send and receive data over the web. Da ...
- ASP.NET MVC数据库初始化
public class DBInitializer:DropCreateDatabaseAlways<BookDBContext> { protected override void S ...
- Angular广播/消息通知的接收与发送
一.在接收页:添加引用: private eventManager: JhiEventManager: 接收通知的方法: // 接收通知(新建.编辑.删除页发送过来的通知) // upmsMenuLi ...
- mui 时间日期控件(浏览器上无法查看,在手机端可以点击)
<head> <meta charset="utf-8"> <meta name="viewport" content=" ...
- 契约式设计(DbC)感想(二)
契约式设计6大原则的理解 在<Design by Contract原则与实践>中,作者定义了契约式设计的6大原则: 区分命令和查询: 将基本查询和派生查询区分开: 针对每个派生查询,设定一 ...
- FlowNet: Learning Optical Flow with Convolutional Networks
作者:嫩芽33出处:http://www.cnblogs.com/nenya33/p/7122701.html 版权:本文版权归作者和博客园共有 转载:欢迎转载,但未经作者同意,必须保留此段声明:必须 ...
- 部分cocoscreator左右移动代码
cc.Class({extends: cc.Component, properties: { // 主角跳跃高度 jumpHeight: 0, // 主角跳跃持续时间 jumpDuration: 0, ...
- Python 保留n位小数
输出a, b 且保留三位小数 a = 2.3456 b = 2.0000 三种方法: round(a, 3)('%.3f' % a)Decimal(a).quantize(Decimal('0.000 ...
- eureka 注册中心
1.eureka版本更新后,pom依赖名称变化 v1.2.7spring-cloud-starter-eureka-server v2.0.0spring-cloud-starter-netflix- ...