Interview----2 sum
题目:输入一个已经按升序排序过的数组和一个数字,
在数组中查找两个数,使得它们的和正好是输入的那个数字。
要求时间复杂度是 O(n)。如果有多对数字的和等于输入的数字,输出任意一对即可。
例如输入数组 1、2、4、7、11、15 和数字 15。由于 4+11=15,因此输出 4 和 11。
分析:
给出两种算法: 1. 枚举第一个,然后二分搜索第二个数。 O(NlgN)
2. 双指针法。 left, right 分别指向首尾,如果 a[left] + a[right] < sum, left++, 如果 >, right --; 否则 找到答案
扩充:找出 3个数,使其和等于 sum. 见 点击打开链接
// copyright @ L.J.SHOU Mar.05, 2014
// 2 sum
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std; // O(NlgN)
void TwoSumVersion1(vector<int> &a, int sum)
{
if(a.size() < 2)
return; for(vector<int>::iterator it = a.begin(); it != a.end(); ++it)
{
if(binary_search(a.begin(), it, sum - *it)
|| binary_search(it+1, a.end(), sum - *it))
{
cout << *it << " " << sum - *it << endl;
return;
}
}
} // two pointers O(N)
void TwoSumVersion2(vector<int> &a, int sum)
{
vector<int>::iterator left(a.begin()), right(a.end()-1); while(left < right)
{
if(*left + *right < sum){
++ left;
}
else if(*left + *right > sum){
-- right;
}
else{
cout << *left << " " << *right << endl;
return;
}
}
} int main(void)
{
int a[]={1,2,4,7,11,15};
vector<int> vec(a, a+sizeof(a)/sizeof(int)); TwoSumVersion1(vec, 15);
TwoSumVersion2(vec, 15); return 0;
}
Interview----2 sum的更多相关文章
- 行列转换文本处理--awk xargs 回顾
awk 数组回顾: 9.1 数组 举例:统计当前主机上每一个TCP连接状态以及每种连接状态的数目[非常实用] # netstat -tan | awk '/^tcp/{STATE[$NF]++}END ...
- Amazon Interview | Set 27
Amazon Interview | Set 27 Hi, I was recently interviewed for SDE1 position for Amazon and got select ...
- [转]Design Pattern Interview Questions - Part 4
Bridge Pattern, Composite Pattern, Decorator Pattern, Facade Pattern, COR Pattern, Proxy Pattern, te ...
- [LintCode] Two Sum 两数之和
Given an array of integers, find two numbers such that they add up to a specific target number. The ...
- [LintCode] Continuous Subarray Sum 连续子数组之和
Given an integer array, find a continuous subarray where the sum of numbers is the biggest. Your cod ...
- [LintCode] Submatrix Sum 子矩阵之和
Given an integer matrix, find a submatrix where the sum of numbers is zero. Your code should return ...
- [LintCode] Minimum Size Subarray Sum 最小子数组和的大小
Given an array of n positive integers and a positive integer s, find the minimal length of a subarra ...
- k Sum | & ||
k Sum Given n distinct positive integers, integer k (k <= n) and a number target. Find k numbers ...
- Lintcode: Interval Sum II
Given an integer array in the construct method, implement two methods query(start, end) and modify(i ...
- Lintcode: Interval Sum
Given an integer array (index from 0 to n-1, where n is the size of this array), and an query list. ...
随机推荐
- 【Linux】rpm -qa 和 rpm -q
查询一个包是否被安装 # rpm -q < rpm package name>列出所有被安装的rpm package # rpm -qae.g. rpm -qa|grep "pc ...
- 【LINUX】Linux学习小结
****xargs命令**** 当需要将参数列表转换成小块分段传递给其他命令时,可以使用xargs命令.栗子如下: 若想在启动lampp之后用kill方式杀掉全部的进程就可以用下面的命令: ps -e ...
- python中的类变量、实例变量
类变量,是各个实例共享的资源,就像中央空调,只有一个,但每个房间(实例)均可享用. 实例变量,是每个实例各自分配使用的变量,每个房间(实例)都有一台空调,供自己使用. class handle(obj ...
- hdu 3853LOOPS (概率DP)
LOOPS Time Limit: 15000/5000 MS (Java/Others) Memory Limit: 125536/65536 K (Java/Others) Total Su ...
- swiftlint升级
~/Opal/iOS/App (mike/lyrics) $ which brew/usr/local/bin/brew~/Opal/iOS/App (mike/lyrics) $ cd /usr/l ...
- 20145236 冯佳 《Java程序设计》第3周学习总结
20145236 <Java程序设计>第3周学习总结 教材学习内容总结 第四章 认识对象 一.面向对象和面向过程 •面向对象是相对面向过程而言 •面向对象和面向过程都是一种思想 •面向过程 ...
- SQL SERVER 2008安装错误(is not a valid login or you do have permission)
在网上搜索查找问题: 原因:[计算机名] 与[账号名称]名称一致. 解决方案: 修改计算机名 桌面 -> 我的电脑 -> 右鍵点击属性 -> 选择计算机名选项卡 -> 更改 - ...
- mysql创建PATH快捷
1.使其临时生效 PATH=$PATH:/usr/local/mysql/bin 2.永久生效 编辑/etc/profile 添加第79列 然后source /etc/profile 3.输入命令m ...
- JS引用类型之——RegExp
前言 之前小猪分享过关于正则表达式的一些内容,具体请看传送门,今天小猪来分享怎么在js中使用正则表达式. 定义 ECMAScript通过RegExp类型来支持正则表达式.使用下面类似Perl的语法,就 ...
- seajs 使用 jquery插件
define(function(require,exports,moudles){ return function(jquery){ (function($) { $.fn.pri= function ...