167. Two Sum II - Input array is sorted【easy】

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) {
vector<int> result; for (int i = , j = numbers.size() -; i <= j;) {
if (numbers[i] + numbers[j] == target) {
result.push_back(i + );
result.push_back(j + );
return result;
}
else if (numbers[i] + numbers[j] > target) {
--j;
}
else if (numbers[i] + numbers[j] < target) {
++i;
}
} return result;
}
};

双指针

解法二:

 class Solution {
public:
vector<int> twoSum(vector<int>& numbers, int target) {
vector<int> result; for (int i = ; i < numbers.size() - ; ++i) {
int start = i + ;
int end = numbers.size() - ;
int temp_target = target - numbers[i]; //binary search
while (start + < end) {
int mid = start + (end - start) / ;
if (numbers[mid] == temp_target) {
result.push_back(i + );
result.push_back(mid + );
return result;
}
else if (numbers[mid] > temp_target) {
end = mid;
}
else if (numbers[mid] < temp_target) {
start = mid;
}
}
if (numbers[start] == temp_target) {
result.push_back(i + );
result.push_back(start + );
return result;
}
if (numbers[end] == temp_target) {
result.push_back(i + );
result.push_back(end + );
return result;
}
} return result;
}
};

二分查找

167. Two Sum II - Input array is sorted【easy】的更多相关文章

  1. 167. Two Sum II - Input array is sorted【Easy】【双指针-有序数组求两数之和为目标值的下标】

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

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

  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

    Difficulty:easy  More:[目录]LeetCode Java实现 Description Given an array of integers that is already sor ...

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

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

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

随机推荐

  1. 【kruscal】【最小生成树】【离线】洛谷 P2266 爱的距离

    建图:每个点向它四周的点连边权为两点点权的差的绝对值的边. 由于有多个需要“施法”的点,所以相当于对每个这样的点,询问与它的距离在T以内的最长边的最小值,即多次询问. 最长边最小之类的,肯定是最小生成 ...

  2. Delphi Delay 延时计数的功能。 下面的方法都是思路,但是没有用在项目上

    procedure Tfrm_InstrumentControl.aa;var CurLength: Word; vTimeLength: Word;begin Screen.Cursor := cr ...

  3. HTML5:绘制图形

    canvas绘图通过属于 canvas 的 JavaScript 方法完成 针对不支持html5的IE浏览器 <!--[if IE]> <script type="text ...

  4. ylbtech-LanguageSamples-COMInteropPart1(COM 互操作 - 第一部分)

    ylbtech-Microsoft-CSharpSamples:ylbtech-LanguageSamples-COMInteropPart1(COM 互操作 - 第一部分) 1.A,示例(Sampl ...

  5. gflops

    这个网站最棒了 http://kyokojap.myweb.hinet.net/gpu_gflops/

  6. JS使用cookie实现DIV提示框只显示一次的方法

    本文实例讲述了JS使用cookie实现DIV提示框只显示一次的方法.分享给大家供大家参考,具体如下: 这里运用JavaScript的cookie技术,控制网页上的提示DIV只显示一次,也就是当用户是第 ...

  7. Unity3D之高级渲染-Shader Forge增强版

    笔者介绍:姜雪伟,IT公司技术合伙人,IT高级讲师,CSDN社区专家.特邀编辑.畅销书作者,国家专利发明人;已出版书籍:<手把手教你架构3D游戏引擎>电子工业出版社和<Unity3D ...

  8. Laravel 5系列教程五:MVC的基本流程

    免费视频教程地址https://laravist.com/series/laravel-5-basic 期间受到很多私事影响,终于还是要好好写写laravel的教程了. 上一篇我们说了数据库和Eloq ...

  9. cakephp事务处理

    使用cakephp框架做开发时,涉及到多个数据表的数据保存,需要使用cakephp的事务处理,查cakephp的说明手册也没看明白,从开发社区中看到了解决的办法,考虑到英文的问题,所以转给大家,以供参 ...

  10. javascript => 方法的简写形式

    https://segmentfault.com/a/1190000002904199 => 是function的简写形式,支持expression 和 statement 两种形式.同时一点很 ...