[leetCode][013] Two Sum 2
题目:
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的更多相关文章
- 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 ...
- LeetCode 1 Two Sum 解题报告
LeetCode 1 Two Sum 解题报告 偶然间听见leetcode这个平台,这里面题量也不是很多200多题,打算平时有空在研究生期间就刷完,跟跟多的练习算法的人进行交流思想,一定的ACM算法积 ...
- [LeetCode] #167# Two Sum II : 数组/二分查找/双指针
一. 题目 1. Two Sum II Given an array of integers that is already sorted in ascending order, find two n ...
- [LeetCode] #1# Two Sum : 数组/哈希表/二分查找/双指针
一. 题目 1. Two SumTotal Accepted: 241484 Total Submissions: 1005339 Difficulty: Easy Given an array of ...
- [array] leetcode - 40. Combination Sum II - Medium
leetcode - 40. Combination Sum II - Medium descrition Given a collection of candidate numbers (C) an ...
- [array] leetcode - 39. Combination Sum - Medium
leetcode - 39. Combination Sum - Medium descrition Given a set of candidate numbers (C) (without dup ...
- LeetCode one Two Sum
LeetCode one Two Sum (JAVA) 简介:给定一个数组和目标值,寻找数组中符合求和条件的两个数. 问题详解: 给定一个数据类型为int的数组,一个数据类型为int的目标值targe ...
- [leetcode]40. Combination Sum II组合之和之二
Given a collection of candidate numbers (candidates) and a target number (target), find all unique c ...
- [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 ...
随机推荐
- System.SysUtils.TMarshaller 与 System.TMarshal
转自:http://www.cnblogs.com/del/archive/2013/06/10/3130974.html TMarshaller(结构) 基于 TMarshal(是有一大堆的 cla ...
- android dialog 模拟新浪、腾讯title弹框效果
http://blog.csdn.net/jj120522/article/details/7764183 首先我们看一下新浪微博的效果(其它就是一个dialog): 点 ...
- 【OpenStack】OpenStack系列13之Nova源码解析与API扩展
学习思路 议程:代码结构-主干流程-分层架构-业务模型-数据库模型-消息模型 分布式架构:Api:横向扩展 rpc:纵向扩展 分层架构:Controller接口层.View/Manager逻辑层 ...
- Java RMI 框架
RMI(即Remote Method Invoke 远程方法调用).在Java中,只要一个类extends了java.rmi.Remote接口,即可成为存在于服务器端的远程对象,供客户端访问并提供一定 ...
- vs2010 调试快捷键
vs2010 调试快捷键 命令名 快捷键 说明 调试.应用代码更改 Alt + F10 启动生成操作,利用它可以通过“编辑并继续”功能应用对正在调试的代码所作的更改. 调试.自动窗口 Ctrl + ...
- C++ 通过WIN32 API 获取逻辑磁盘详细信息
众所周知,在微软的操作系统下编写应用程序,最主要的还是通过windows所提供的api函数来实现各种操作的,这些函数通常是可以直接使用的,只要包含windows.h这个头文件, 下载源文件 今天我们主 ...
- codeforces A. Puzzles 解题报告
题目链接:http://codeforces.com/problemset/problem/337/A 题意:有n个学生,m块puzzles,选出n块puzzles,但是需要满足这n块puzzles里 ...
- MFC RadioButton
添加一组RadioButton 多个radio button,IDC_RADIO1,IDC_RADIO2,IDC_RADIO3 ..将IDC_RADIO1的Group属性选择上,其他不要选Group属 ...
- [Linux] Linux进程PID散列表
linux系统中每个进程由一个进程id标识,在内核中对应一个task_struct结构的进程描述符,系统中所有进程的task_struct通过链表链接在一起,在内核中,经常需要通过进程id来获取进程描 ...
- [Android UI] shape和selector的结合使用
shape和selector是Android UI设计中经常用到的,比如我们要自定义一个圆角Button,点击Button有些效果的变化,就要用到shape和selector.可以这样说,shape和 ...