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的更多相关文章

  1. 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 ...

  2. [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 ...

  3. 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 ...

  4. (双指针 二分) 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 ...

  5. 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 ...

  6. ✡ 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 ...

  7. 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 ...

  8. 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 ...

  9. 167. Two Sum II - Input array is sorted - LeetCode

    Question 167. Two Sum II - Input array is sorted Solution 题目大意:和Two Sum一样,这里给出的数组是有序的 思路:target - nu ...

随机推荐

  1. C#扩展方法学习

    扩展方法的本质是什么,详细见此文 C#扩展方法,爱你在心口难开 重点如下:扩展方法使您能够向现有类型“添加”方法,而无需创建新的派生类型.重新编译或以其他方式修改原始类型.扩展方法是一种特殊的静态方法 ...

  2. 嵌套查询--------关联一对多关系----------collection

    参考来源:   http://www.cnblogs.com/LvLoveYuForever/p/6689577.html <resultMap id="BaseResultMap&q ...

  3. 421 Maximum XOR of Two Numbers in an Array 数组中两个数的最大异或值

    给定一个非空数组,数组中元素为 a0, a1, a2, … , an-1,其中 0 ≤ ai < 231 .找到 ai 和aj 最大的异或 (XOR) 运算结果,其中0 ≤ i,  j < ...

  4. python_基础部分(1)

    第1章 Python介绍 1.1 基础介绍 l  代码:代码的出现是为了解决生活中的问题 l  编译解释器:目的是让解释器将代码翻译成计算机可识别的语言 l  编程语言:按照一定规则写出来的语言, C ...

  5. [转]ASP.net MVC 2 自定义模板来显示数据

    本文转自:http://blog.163.com/liaojunbo@126/blog/static/1394892972012113104653651/ 在ASP.net MVC 2中,一个很有意思 ...

  6. jQuery Ajax使用实例

    <script src="http://cdn.bootcss.com/jquery/1.11.2/jquery.js"></script> <scr ...

  7. (三)SpringIoc之初了解

    IoC:Inverse of Control(控制反转) 读作"反转控制",更好理解,不是什么技术,而是一种设计思想,就是将原本在程序中手动创建对象的控制权,交由Spring框架来 ...

  8. 内置函数isinstance和issubclass

    1. isinstance(obj,class) 判断对象obj是不是由class生成的对象. class Foo: pass obj=Foo() print(isinstance(obj,Foo)) ...

  9. 分组密码_计数器(CTR)模式_原理及java实现

    一.原理: CTR模式是一种通过将逐次累加的计数器进行加密来生成密钥流的流密码,在CTR模式中,每个分组对应一个逐次累加的计数器,并通过对计数器进行加密来生成密钥流.最终的密文分组是通过将计数器加密得 ...

  10. 【Linux】 CentOS免密登录

    #sudo ssh-keygen -t rsa #sudo ssh-copy-id ${ipAddress}