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. 19/03/13python学习笔记

    1.变量命名 name1 = 1 name2 = "sunj" 2.命名变量的规则 (1.变量是字母.数字.下划线的组合(2.不能以数字开头(3.不能用关键词命名变量(4.变量中间 ...

  2. s2第六章继承和多态

    public class Employee { //年龄 public int Age { get; set; } //性别 public Gender Gender { get; set; } // ...

  3. MySQL Execution Plan--NOT EXISTS子查询优化

    在很多业务场景中,会使用NOT EXISTS语句来确保返回数据不存在于特定集合,部分场景下NOT EXISTS语句性能较差,网上甚至存在谣言"NOT EXISTS无法走索引". 首 ...

  4. fork exec source的区别

    参考:http://www.cnblogs.com/bkygg/p/5023072.html 1:fork  运行的时候开一个sub_shell 执行调用的脚本,sub_shell执行的时候,pare ...

  5. Android的发展历史

    Android一词最早出现于法国作家利尔亚当(Auguste Villiers de l’Isle-Adam)在1886年发表的科幻小说<未来夏娃>(L’ève future)中.他将外表 ...

  6. 自己动手,丰衣足食!Python3网络爬虫实战案例

    本教程是崔大大的爬虫实战教程的笔记:网易云课堂 Python3+Pip环境配置 Windows下安装Python: http://www.cnblogs.com/0bug/p/8228378.html ...

  7. PHP 获取数组是几维数组

    // 判断数组是几维数组$data = array(); // 是你要判断的数组$al = array(0);function aL($data,&$al,$level=0){ if(is_a ...

  8. oracle user_tables没有新创建的表的问题

    oracle 新创建表后,在user_tables没有,在user_tab_columns也没有,暂时未找到办法

  9. 学习笔记之Model selection and evaluation

    学习笔记之scikit-learn - 浩然119 - 博客园 https://www.cnblogs.com/pegasus923/p/9997485.html 3. Model selection ...

  10. [UE4]Border

    Border: 边界; 边; 镶边; 包边; Border也是一个容器,只能包含一个子元素. 一.添加一个名为testBorder的UserWidget,添加一个Border到默认成Canvas Pa ...