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. jquery.guide.js 新手指引

    /*! * by xyb * 新版上线时候的黑色半透明镂空遮罩指引效果实现jQuery小插件 * 兼容到IE8+ * MIT使用协议,使用时候保留版权 * */ $.guide = function ...

  2. 笔试算法题(39):Trie树(Trie Tree or Prefix Tree)

    议题:TRIE树 (Trie Tree or Prefix Tree): 分析: 又称字典树或者前缀树,一种用于快速检索的多叉树结构:英文字母的Trie树为26叉树,数字的Trie树为10叉树:All ...

  3. mySQL--找出各科最高成绩

    //找出各科最高成绩create table gradeMax(id int primary key auto_increment,name varchar(20),subject varchar(2 ...

  4. KBE_运作流程

    图片来自官方手册:Switch Fabric:交换机网络,根据网络环境的不同而不同,根据用户自己的情况进行配置,不属于引擎范畴. 运作流程 Client连接Loginapp通过API连接,如下示例: ...

  5. python中的句柄操作

    python中的句柄操作 制作人:全心全意 通过窗口标题获取句柄 import win32gui hld = win32gui.FindWindow(None,u"Adobe Acrobat ...

  6. 树莓派 - wiringPi

    wiringPi其实和BCM2835 library类似,也是通过memmap, IOmap来实现在用户空间直接操作底层寄存器 wiringPi http://wiringpi.com/ Wiring ...

  7. Python:webshell 跳板机审计服务器

    1.修改paramiko源码包实现 https://github.com/paramiko/paramiko/tree/1.10.1 下载源码包 unzip paramiko-1.10.1.zip p ...

  8. Final Battle #1 K题 Fire game

    Description Fat brother and Maze are playing a kind of special (hentai) game on an N*M board (N rows ...

  9. 解决在使用Amoeba遇到的问题

    最近有同行在使用Amoeba 的过程中多少遇到了一些问题. 总结一下遇到问题的解决方法: 1.读写分离的时候设置的在queryRouter中设置无效? 读写分离配置的优先级别:        1)满足 ...

  10. android开发里跳过的坑——listview不显示

    在蓝牙回调接口public void onLeScan(BluetoothDevice device, int arg1, byte[] arg2)里面调用adpter.notifyDataSetCh ...