题目:

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.

    Input: numbers={2, 7, 11, 15}, target=9
    Output: index1=1, index2=2

题意:请首先看"[leetCode][012] Two Sum 1",大家发现什么异同了没(没发现的童鞋面壁去),这道题目仅仅更改了输入条件,Input的数组为已经排序的数组,并且呈现升序。输入条件变严格,我们依然可以使用前一篇问丈夫中的方法进行。那这里既然为升序,我们能不能不使用前一种方法而又能够达到O(n)时间复杂度呢? 答案是必然的。

key Point:

  数组既然排序,元素之前的大小关系相对确定,存在的这一对元素必然处于相对固定的位置,我们可以使用两个游标,一个从头部遍历,一个从尾部遍历,两者所指元素之和如果偏大,调整后面的游标,相反则调整前面的游标。

坑:

  由于两个int之和有可能出现INT_MAX的情况,所以如果输入类型给定int,则我们需要使用long long类型来表示才能避免出现溢出的情况。

解答:

以下为C++实现代码:

 class Solution{
public:
std::vector<int> twoSum(std::vector<int> &numbers, int target){
std::vector<int> vecRet;
int nLeft = ;
int nRight = numbers.size() - ; while (nLeft < nRight){
// 小心两个int之和溢出,使用long long类型
long long int nAdd = numbers[nLeft] + numbers[nRight];
if (nAdd == target){
vecRet.push_back(nLeft + );
vecRet.push_back(nRight + ); return vecRet;
}
else if (nAdd > target){
nRight--;
}
else if (nAdd < target){
nLeft++;
}
} return vecRet;
}
};

运行结果:

  

希望各位看官不吝赐教,小弟感恩言谢。

[leetCode][013] Two Sum 2的更多相关文章

  1. Java for LeetCode 216 Combination Sum III

    Find all possible combinations of k numbers that add up to a number n, given that only numbers from ...

  2. LeetCode 1 Two Sum 解题报告

    LeetCode 1 Two Sum 解题报告 偶然间听见leetcode这个平台,这里面题量也不是很多200多题,打算平时有空在研究生期间就刷完,跟跟多的练习算法的人进行交流思想,一定的ACM算法积 ...

  3. [LeetCode] #167# Two Sum II : 数组/二分查找/双指针

    一. 题目 1. Two Sum II Given an array of integers that is already sorted in ascending order, find two n ...

  4. [LeetCode] #1# Two Sum : 数组/哈希表/二分查找/双指针

    一. 题目 1. Two SumTotal Accepted: 241484 Total Submissions: 1005339 Difficulty: Easy Given an array of ...

  5. [array] leetcode - 40. Combination Sum II - Medium

    leetcode - 40. Combination Sum II - Medium descrition Given a collection of candidate numbers (C) an ...

  6. [array] leetcode - 39. Combination Sum - Medium

    leetcode - 39. Combination Sum - Medium descrition Given a set of candidate numbers (C) (without dup ...

  7. LeetCode one Two Sum

    LeetCode one Two Sum (JAVA) 简介:给定一个数组和目标值,寻找数组中符合求和条件的两个数. 问题详解: 给定一个数据类型为int的数组,一个数据类型为int的目标值targe ...

  8. [leetcode]40. Combination Sum II组合之和之二

    Given a collection of candidate numbers (candidates) and a target number (target), find all unique c ...

  9. [LeetCode] 437. Path Sum III_ Easy tag: DFS

    You are given a binary tree in which each node contains an integer value. Find the number of paths t ...

随机推荐

  1. 《转》.NET开源核心运行时,且行且珍惜

    转载自infoQ 背景 InfoQ中文站此前报道过,2014年11月12日,ASP.NET之父.微软云计算与企业级产品工程部执行副总裁Scott Guthrie,在Connect全球开发者在线会议上宣 ...

  2. 【转】SQL删除重复记录,只保留其中一条

    SQL:删除重复数据,只保留一条用SQL语句,删除掉重复项只保留一条在几千条记录里,存在着些相同的记录,如何能用SQL语句,删除掉重复的呢 1.查找表中多余的重复记录,重复记录是根据单个字段(peop ...

  3. Sybase IQ导出文件的几种方式

    IQ有四种方法,将表的数据导出为文本文件:1.重定向 SELECT * FROM TABLE1 ># D:MYDATATABLE1.TXT -- 文件生成在执行语句的客户端上 2.通过选项导出 ...

  4. Ubuntu 用户安装 MATE

      MATE 是经典桌面 Gnome 2 的分支,该桌面按照 Windows 用户操作习惯设计,适合于 Windows 转投 Linux 的初级用户,MATE 做了功能改进和新增功能.如:增加窗口管理 ...

  5. CSS 样式显示为小手

    因为工作需要把鼠标放上去显示小手形状, css样式如下: style="cursor:hand"    部分浏览器支持 style="cursor:pointer&quo ...

  6. HDU 4315 Climbing the Hill (阶梯博弈转尼姆博弈)

    Climbing the Hill Time Limit: 1000MS   Memory Limit: 32768KB   64bit IO Format: %I64d & %I64u Su ...

  7. 优秀前端工程师应该掌握的内容(转自:github)

    程序 标准规范 ECMAScript HTTP 知识储备 作用域/闭包 数据结构 算法 编程范式 函数式 面向对象 基于原型 面向方面 设计模式 软件架构 MVC MVVM 安全 XSS CSRF 富 ...

  8. Fresco 源码分析(二) Fresco客户端与服务端交互(2) Fresco.initializeDrawee()分析 续

    4.2.1.2 Fresco.initializeDrawee()的过程 续 继续上篇博客的分析Fresco.initializeDrawee() sDraweeControllerBuilderSu ...

  9. Nginx开启Gzip压缩大幅提高页面加载速度

    [root@12 conf]# vi nginx.conf gzip on;#开启GZIP gzip_min_length 1k; #不压缩临界值,大于1K的才压缩,一般不用改 gzip_buffer ...

  10. C# WebProxy POST 或者 GET

    代理服务器无账号和密码的代理服务器: //创建请求 HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url); //实例化一个We ...