2014-05-12 07:31

题目链接

原题:

I have heard this question many times in microsoft interviews. Given two arrays find the intersection of those two arrays. Besides using hash table can we attain the same time complexity that is O(m+n) by using some other approach.

题目:给定两个数组,计算出他们的交集。要求线性时间完成。

解法1:出题者问能否在不用哈希的条件下,用线性时间完成。数组本身不一定是有序的,所以我没想出不用哈希的线性算法。一种可行的解法,是先排序两个数组,然后进行归并,取其交集。显然这种算法的复杂度主要来自排序。

代码:

 // http://www.careercup.com/question?id=24308662
// nobody said the elements in both arrays are unique, why would bitset work?
#include <algorithm>
#include <iostream>
#include <vector>
using namespace std; class Solution {
public:
void mergeIntersection(vector<int> &a1, vector<int> &a2, vector<int> &intersect) {
sort(a1.begin(), a1.end());
sort(a2.begin(), a2.end()); int i, j;
int n1, n2; i = ;
j = ;
n1 = (int)a1.size();
n2 = (int)a2.size();
intersect.clear();
while (i < n1 && j < n2) {
if (a1[i] < a2[j]) {
++i;
} else if (a1[i] > a2[j]) {
++j;
} else {
intersect.push_back(a1[i]);
++i;
++j;
}
}
};
}; int main()
{
int n1, n2, n;
vector<int> a1, a2;
vector<int> intersect;
int i;
Solution sol; while (cin >> n1 >> n2 && (n1 > && n2 > )) {
a1.resize(n1);
a2.resize(n2);
for (i = ; i < n1; ++i) {
cin >> a1[i];
}
for (i = ; i < n2; ++i) {
cin >> a2[i];
}
sol.mergeIntersection(a1, a2, intersect); cout << '{';
n = (int)intersect.size();
for (i = ; i < n; ++i) {
i ? (cout << ' '), : ;
cout << intersect[i];
}
cout << '}' << endl;
} return ;
}

解法2:用哈希来搞定,可以在线性时间内完成。统计两个数组中每个值出现的次数,取较少的次数作为交集。比如A[]中有3个“1”,B[]中有5个“1”,那么交集就有3个“1”。

代码:

 // http://www.careercup.com/question?id=24308662
// nobody said the elements in both arrays are unique, why would bitset work?
#include <algorithm>
#include <iostream>
#include <vector>
#include <unordered_map>
using namespace std; class Solution {
public:
void mergeIntersection(vector<int> &a1, vector<int> &a2, vector<int> &intersect) {
if (a1.size() > a2.size()) {
mergeIntersection(a2, a1, intersect);
return;
}
unordered_map<int, pair<int, int> > um;
int n1, n2;
int i; n1 = (int)a1.size();
n2 = (int)a2.size();
unordered_map<int, pair<int, int> >::iterator it; for (i = ; i < n1; ++i) {
it = um.find(a1[i]);
if (it == um.end()) {
um[a1[i]] = make_pair(, );
} else {
++it->second.first;
}
} for (i = ; i < n2; ++i) {
it = um.find(a2[i]);
if (it != um.end()) {
++it->second.second;
}
} intersect.clear();
for (it = um.begin(); it != um.end(); ++it) {
n1 = min(it->second.first, it->second.second);
for (i = ; i < n1; ++i) {
intersect.push_back(it->first);
}
} um.clear();
};
}; int main()
{
int n1, n2, n;
vector<int> a1, a2;
vector<int> intersect;
int i;
Solution sol; while (cin >> n1 >> n2 && (n1 > && n2 > )) {
a1.resize(n1);
a2.resize(n2);
for (i = ; i < n1; ++i) {
cin >> a1[i];
}
for (i = ; i < n2; ++i) {
cin >> a2[i];
}
sol.mergeIntersection(a1, a2, intersect); cout << '{';
n = (int)intersect.size();
for (i = ; i < n; ++i) {
i ? (cout << ' '), : ;
cout << intersect[i];
}
cout << '}' << endl;
} return ;
}

Careercup - Microsoft面试题 - 24308662的更多相关文章

  1. Careercup - Microsoft面试题 - 6314866323226624

    2014-05-11 05:29 题目链接 原题: Design remote controller for me. 题目:设计一个遥控器. 解法:遥控什么?什么遥控?传统的红外线信号吗?我只能随便说 ...

  2. Careercup - Microsoft面试题 - 6366101810184192

    2014-05-10 22:30 题目链接 原题: Design database locks to allow r/w concurrency and data consistency. 题目:设计 ...

  3. Careercup - Microsoft面试题 - 5700293077499904

    2014-05-12 00:02 题目链接 原题: For a given map (ie Bing map) given longitude/latitude/ how would you desi ...

  4. Careercup - Microsoft面试题 - 5204967652589568

    2014-05-11 23:57 题目链接 原题: identical balls. one ball measurements ........ dead easy. 题目:9个看起来一样的球,其中 ...

  5. Careercup - Microsoft面试题 - 5175246478901248

    2014-05-11 23:52 题目链接 原题: design an alarm clock for a deaf person. 题目:为聋人设计闹钟? 解法:聋人听不见,那么闪光.震动都可行.睡 ...

  6. Careercup - Microsoft面试题 - 5718181884723200

    2014-05-11 05:55 题目链接 原题: difference between thread and process. 题目:请描述进程和线程的区别. 解法:操作系统理论题.标准答案在恐龙书 ...

  7. Careercup - Microsoft面试题 - 5173689888800768

    2014-05-11 05:21 题目链接 原题: Complexity of a function: int func_fibonacci ( int n) { ) { return n; } el ...

  8. Careercup - Microsoft面试题 - 6282862240202752

    2014-05-11 03:56 题目链接 原题: Given an integer array. Perform circular right shift by n. Give the best s ...

  9. Careercup - Microsoft面试题 - 5428361417457664

    2014-05-11 03:37 题目链接 原题: You have three jars filled with candies. One jar is filled with banana can ...

随机推荐

  1. jq动态增加的button标签click回调失效的问题,即动态增加的button标签绑定事件$("button.class").click(function)无效

    对于新增加的页面元素,改变了页面结构,如果是使用老办法$("button.class").click(function)去监听新的button标签事件,会失效. 笔者的应用是文字的 ...

  2. 兼容ie8的圆形进度条

    主要是利用html5中的svg 画出圆形进度条 并且兼容ie8 https://github.com/GainLoss/Circular-progress-bar

  3. [VC]线程

    是进程中的一个实体,是被系统独立调度和分派的基本单位,线程自己不拥有系统资源,只拥有一点在运行中必不可少的资源,但它可与同属一个进程的其它线程共 享进程所拥有的全部资源.一个线程可以创建和撤消另一个线 ...

  4. IOS 长按+轻扫(手势识别)

    @interface NJViewController () @property (weak, nonatomic) IBOutlet UIView *customView; @end @implem ...

  5. 问题 F: 等比数列

    问题 F: 等比数列 时间限制: 1 Sec  内存限制: 64 MB提交: 2699  解决: 1214[提交][状态][讨论版][命题人:外部导入] 题目描述 已知q与n,求等比数列之和: 1+q ...

  6. 2017.12.6 计算机算法分析与设计---------Fibonacci数列

    (1)题目: 无穷数列1,1,2,3,5,8,13,21,34,55,--,称为Fibonacci数列.它可以递归地定义为: 第n个Fibonacci数可递归地计算如下: int fibonacci( ...

  7. Linux命令安装vnc服务端与vnc的客户端

    第一歩:运行命令 yum install tigervnc-server -y 第二歩:安装telnet 第三歩:运行vncserver,创建桌面 vncserver -kill :1  删除桌面1的 ...

  8. Java 程序设计总复习题

    Java程序设计总复习题 1.编写一个Java程序在屏幕上输出“你好!”. //programme name Helloworld.java public class Helloworld { pub ...

  9. css与html结合四种方式

    方式一:每个标签加一个属性法 <div style="background-color:red;color:green;"></div> 方式二:head中 ...

  10. LGTB 学分块

    总时间限制:  10000ms 单个测试点时间限制:  1000ms 内存限制:  65536kB 描述 LGTB 最近在学分块,但是他太菜了,分的块数量太多他就混乱了,所以只能分成 3 块 今天他得 ...