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. Ubuntu16.04安装YouCompleteMe

    1.要求vim的版本在7.4.143以上,支持python2/3,通过vim --version查看. 2.下载源码:  https://github.com/Valloric/YouComplete ...

  2. 用turtle画图

    turtle是python自带一个寓教于乐的小乌龟,可以控制乌龟移动(机器人),可以留下足迹. turtledemo里有许多官方例子.刚才随性而发做,看了介绍随手画了一个,有点像自动原包机,通过简单的 ...

  3. 登录ssh提示:ssh_exchange_identification: read: Connection reset by peer error

    vim /etc/hosts.allow 添加 sshd : ALL

  4. Java_集合面试题

    Java_集合面试题 0.链表,队列和栈的区别? 链表是一种存储结构,指得是存储时候除了要存储数据元素之外,还要用数据元素一起的另外空间存储数据元素的关系. 队列和栈都是线性表,属于逻辑结构范畴,都是 ...

  5. Delphi2009之TImage

    TPngImage原来是SourceFroge上的一个开源项目,现在突然消失了,为什么呢?Nick 在他的博客上写到:TPNGImage被CodeGear/Embarcadero收购了,现在直接就是D ...

  6. [蓝桥杯]PREV-7.历届试题_连号区间数

    问题描述 小明这些天一直在思考这样一个奇怪而有趣的问题: 在1~N的某个全排列中有多少个连号区间呢?这里所说的连号区间的定义是: 如果区间[L, R] 里的所有元素(即此排列的第L个到第R个元素)递增 ...

  7. [蓝桥杯]ALGO-186.算法训练_P0501

    输入两个无符号整数x, y, 用位操作实现无符号整数的乘法运算.不用考虑整数的溢出. 输入: 输出: 题目描述 代码如下: #include <stdio.h> #include < ...

  8. IIS6.0+win2003部署MVC网站的一些问题

    安装iis,framework环境不谈.MVC网站部署 步骤: 1.为程序新建一个应用程序池(将default的那个程序池作为模板就可以了) 2.web服务扩展一些启用一些必要的服务 3.新建网站 描 ...

  9. 知识点:Mysql 基本用法之函数

    函数 MySQL中提供了许多内置函数 例如: sql 内置函数: 一.数学函数 ROUND(x,y) 返回参数x的四舍五入的有y位小数的值 RAND() 返回0到1内的随机值,可以通过提供一个参数(种 ...

  10. 【Selenium】各种方式在选择的时候应该怎么选择

    最后再总结一下,各种方式在选择的时候应该怎么选择: 1. 当页面元素有id属性时,最好尽量用id来定位.但由于现实项目中很多程序员其实写的代码并不规范,会缺少很多标准属性,这时就只有选择其他定位方法. ...