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. ...
随机推荐
- left join与on,where 结合一起用的异同
I.数据库在通过连接两张或多张表来返回记录时,都会生成一张中间的临时表,然后再将这张临时表返回给用户. 在使用left join时,on和where条件的区别如下: 1. on条件是在生成临时表时使用 ...
- [Java] 获取本月周次和日期时间段信息
package com.wdcloud.monitoring.common; import java.text.SimpleDateFormat; import java.util.ArrayList ...
- 琐碎的总结 css jQuery js 等等。。。
jQuery outerWidth(true)支持ie7 不错不错css BFC a {display:inline-block} 有用,block不是很有效果 ...
- mysql 初始密码 设置
mysql root 密碼的設置方法 shell> mysql -u root mysql mysql> SET PASSWORD FOR root@localhost=PASSWORD( ...
- inout
在函数声明时就用inout代替var 这样以后可以在函数内部修改外面的值 类似于C语言的传入指针 func change (inout num:Int) { num = 10 } var a = 2 ...
- WCF学习
WCF初探-1:认识WCF MQ与Webservice的区别 Webservice 和MQ(MessageQueue)都是解决跨平台通信的常用手段,两者有哪些区别呢? 个人认为最本质的区别在于 Web ...
- 给Eclipse中hibernate.cfg.xml配置文件加提示
在hibernate框架需要的jar包中找到hibernate3.jar,并用压缩软件打开,如图: 2 选择org文件夹--打开下一级文件夹 3 点击类型,方便找到dtd文件,下拉查看dtd文件,有两 ...
- query attr prop区别
大家都知道有的浏览器只要写disabled,checked就可以了,而有的要写成disabled = "disabled",checked="checked", ...
- WordPress的神器
导读 Markdown 是一种简单的标记语言,旨在帮助你花费更小的代价来格式化纯文本文档.在 WordPress 下你可以使用 HTML 或者可视化编辑器来格式化你的文档,但是使用 markdown ...
- 最新电Call记录统计-full hash join用法
declare @time datetime set @time='2016-07-01' --最新的电Call记录统计查询--SELECT t.zuoxi1,t.PhoneCount,t.Phone ...