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. ...
随机推荐
- java.lang.IllegalArgumentException: addChild: Child name '/SSHE' is not unique
错误信息: Caused by: java.lang.IllegalArgumentException: addChild: Child name '/SSHE' is not unique ...
- 20145236 《Java程序设计》实验三实验报告
实验三实验报告 和张亚军同学一组: 实验三实验报告
- oAuth协议学习
我们的项目需要为一个认证网站开发一套API,这些API可以提供给很多公司来调用,但是公司在调用之前,必须先做授权认证,由此接触到了oAuth协议. 以下内容来自网络整理 定义 OAUTH协议为用户资源 ...
- FaceBook Twitter实习生简历求内推
写在博客里面吧. 有一个朋友,男,博士在读,研究方向为图像处理,计算机视觉相关. 想在在读期间有一些海外实习经历.不知道哪位博友,有相关的人脉,求内推啊.内推成功的话请吃大餐,哈哈!
- MySQL 建库、建用户及建表事项
1,MySQL建库语句比较简单,一句话: create database tppamltest3 2,创建用户及授权: insert into mysql.user(Host,User,Passwor ...
- mrg_myIsam分表引擎用法
CREATE TABLE `test`.`article_0` ( `id` BIGINT( 20 ) NOT NULL , `subject` VARCHAR( 200 ) NOT NULL , ` ...
- document.cookie的使用
设置cookie每个cookie都是一个名/值对,可以把下面这样一个字符串赋值给document.cookie:document.cookie="userId=828";如果要一次 ...
- bzoj 1926: [Sdoi2010]粟粟的书架
#include<cstdio> #include<iostream> #define N 201 #define M 500008 using namespace std; ...
- NDK JNI 的关键点
1.System.loadLibrary 的名字是在Android.mk里面设定的 LOCAL_MODULE := httpdown,MODULE 后面跟的就是了 2.如何正确调用到关键 ...
- hdu 4616 Game
http://acm.hdu.edu.cn/showproblem.php?pid=4616 要记录各种状态的段 a[2][4] a[0][j]表示以trap为起点一共有j个trap的最优值 a[1 ...