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) {
int n = numbers.size();
vector<int>w();
for (int i = ; i < n; ++i) {
int x = target - numbers[i];
int p = lower_bound(numbers.begin() + i + ,numbers.end(),x) - numbers.begin();
if (p < n && (numbers[i] + numbers[p] == target)) {
w[] = i + ;
w[] = p + ;
break;
}
}
return w;
}
};

另一种解法更巧妙 two-pointer

class Solution {
public:
vector<int> twoSum(vector<int>& numbers, int target) {
int lo=, hi=numbers.size()-;
while (numbers[lo]+numbers[hi]!=target){
if (numbers[lo]+numbers[hi]<target){
lo++;
} else {
hi--;
}
}
return vector<int>({lo+,hi+});
}
};

167. Two Sum II - Input array is sorted (二分ortwo-pointer)的更多相关文章

  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. 167. Two Sum II - Input array is sorted (Array)

    Given an array of integers that is already sorted in ascending order, find two numbers such that the ...

随机推荐

  1. js判断图片是否有效

    var ImgObj=new Image(); ImgObj.src= 'http://192.168.10.6:8082/3D/SERVER_1_DELL_880.jpg'; if(ImgObj.f ...

  2. google查找笔记

    1.可以选择MDN.WIKI等权威的网址资料

  3. Codeforces450 B. Jzzhu and Sequences

    B. Jzzhu and Sequences time limit per test 1 second memory limit per test 256 megabytes input standa ...

  4. Oracle 实现查询不区分大小写(SQL实现)

    转为小写  LOWER('ABC') 结果 abc转为大写  UPPER('aBc') 结果 ABC 将数据库字段数据和前台接受的值全部转换为大写或者小写 例: select * from table ...

  5. hibernate-validator验证请求参数

    开发接口要进行请求参数内容格式校验,比如在接收到请求参数后依次需要进行数据内容判空.数据格式规范校验等,十分麻烦,于是尝试用hibernate-validator进行参数校验,简单记录一下使用步骤: ...

  6. 九度oj 题目1196:成绩排序

    题目1196:成绩排序 时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:5192 解决:1792 题目描述: 用一维数组存储学号和成绩,然后,按成绩排序输出. 输入: 输入第一行包括一个整数 ...

  7. [NOIP2006] 普及组

    明明的随机数 STL真是偷懒神器 /*by SilverN*/ #include<algorithm> #include<iostream> #include<cstri ...

  8. noip 2011

    铺地毯 题目描述 为了准备一个独特的颁奖典礼,组织者在会场的一片矩形区域(可看做是平面直角坐标系的第一象限)铺上一些矩形地毯.一共有 n 张地毯,编号从 1 到n .现在将这些地毯按照编号从小到大的顺 ...

  9. 所有的异常都要使用try catch 语句捕获?

    在开发应用程序过程中必须检测代码可能发生的错误并进行正确的处理,这个在理想的情况下,应用程序中的每行 代码都按照预想的执行,要用到的每种资源总是可以利用,但是在实际的开发过程中,写代码难免会出错,或是 ...

  10. JSP中访问数据库

    在JSP中访问数据库使用的是JSTL标签,本文不按照http://wiki.jikexueyuan.com/project/jsp/database-access.html此方法进行实践,而是采用之前 ...