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.

Note:

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

Input: numbers = [2,7,11,15], target = 9
Output: [1,2]
Explanation: The sum of 2 and 7 is 9. Therefore index1 = 1, index2 = 2.

class Solution {
public:
vector<int> twoSum(vector<int>& numbers, int target) {
for (int i = ; i < numbers.size(); i++){
if(target - numbers[i] < numbers[i]) break; //second data smaller than current data
if(binarySearch(numbers, i+, numbers.size()-, target-numbers[i])){
vector<int> ret;
ret.push_back(i+);
ret.push_back(pos+);
return ret;
} }
} bool binarySearch(vector<int>& numbers, int start, int end, int target){
if(start > end) return false; int mid = start + (end - start )/;
if(numbers[mid] > target) return binarySearch(numbers, start, mid-, target);
else if (numbers[mid] < target) return binarySearch(numbers, mid+, end, target);
else{
pos = mid;
return true;
}
} private:
int pos; //second data index
};

167. Two Sum II - Input array is sorted (Array)的更多相关文章

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

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

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

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

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

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

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

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

  9. [LeetCode&Python] Problem 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 ...

随机推荐

  1. Introduction of filter in servlet

    官方给出的Filter的定义是在请求一个资源或者从一个资源返回信息的时候执行过滤操作的插件.我们使用过滤起最多的场景估计就是在请求和返回时候的字符集转换,或者权限控制,比如一个用户没有登录不能请求某些 ...

  2. 关于PostmanURL中不能传递中文的问题

    众所周知,中文乱码等问题在开发过程中让人厌烦,本人最近在使用Postman插件测试web的时候,在请求中添加了中文,结果请求错误 截图如下: 原因:不能直接处理中文,需要自行转化 解决: 1.新建一个 ...

  3. Fabric实例

    Fabric的官网  http://fabric-chs.readthedocs.io/zh_CN/chs/index.html 参考<Python自动化运维 技术与最佳实践>   1:查 ...

  4. [转]Python3《机器学习实战》学习笔记(一):k-近邻算法(史诗级干货长文)

    转自http://blog.csdn.net/c406495762/article/details/75172850 版权声明:本文为博主原创文章,未经博主允许不得转载.   目录(?)[-] 一 简 ...

  5. 文件处理,三元操作符,seek()函数,迭代函数和列表解析,reduce函数

    1.文件读取方类型 r,r+,w,x,a, r,读文件 w,写文件,文件内容全部删除,并将新内容从第一行开始赋值 x,写文件,只有文件不存在,可写,文件存在,报错 a,在文件莫问追加信息 r+,w+, ...

  6. Scrapy学习篇(十三)之scrapy+selenum获取网站cookie并保存带本地

    参考:https://www.cnblogs.com/small-bud/p/9064674.html 和selenium登录51job的例子

  7. 并发之java.util.concurrent.atomic原子操作类包

    15.JDK1.8的Java.util.concurrent.atomic包小结 14.Java中Atomic包的原理和分析 13.java.util.concurrent.atomic原子操作类包 ...

  8. Power Designer 转C#实体类方法

    1.打开Power Designer菜单 Tools,选择如图    2.弹出方框中选择PD安装目录下的如图地址 3.object language选择正确目录后,可选如图语言,如C#.再填写name ...

  9. Linux下部署 apache+jdk+tomcat

    1.输入下面的命令安装apache2: ubuntu@VM-164-86-ubuntu:sudo apt-get install apache2 当提示“您希望继续执行吗?”时  输入 Y 然后等待安 ...

  10. Android控件使用FragmentTabHost,切换Fragment;

    大部分APP的主界面都很类似,要么底部导航的,要么就是侧滑菜单,还有底部导航+侧滑菜单的:底部导航实现大概有几种方式: TabHost+Fragment RadioGroup+Fragment Fra ...