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. echarts使用中的那些事儿(一)

    近来由于有几个小项目要用到echarts里的一些图,不得不使用,可是要完全按照自己的意愿来,要对它有些了解,要翻阅资料,遂有以下小结: 1.最开始第一步是把数据调出来就行,能在图上显示就成,以下是最开 ...

  2. 设置mapcontrol的鼠标样式

    http://blog.itpub.net/14999074/viewspace-586515/ mapcontrol的鼠标样式 this.axMapControl1.MousePointer=esr ...

  3. spring-framework-3.0.2RELEASE之后为啥没有依赖包了?

    缘起:莫莫接到新任务要学习spring mvc,于是在网上找了个demo文档跟着一起做.这个是学习的网址: http://www.open-open.com/doc/view/a6462d9a2e2b ...

  4. HDU 1059 Dividing 分配(多重背包,母函数)

    题意: 两个人共同收藏了一些石头,现在要分道扬镳,得分资产了,石头具有不同的收藏价值,分别为1.2.3.4.5.6共6个价钱.问:是否能公平分配? 输入: 每行为一个测试例子,每行包括6个数字,分别对 ...

  5. 安装express

    就目前来说安装express需要走几个步骤,要不就会出现在检查版本的时候就会出现,expres不是内部的命令或者是这种 安装的步骤: 1. 先是输入npm install -g express-gen ...

  6. HDU2612 BFS

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2612 , 一道比较简单的广搜(BFS)题目. 算法: 设置两个dist[][]数组,记录Y和M到几个K ...

  7. StringBuffer是可变的还是不可变的?

    前言:我们知道String类的修饰符是final,其char[] value也是由final修饰的,每次给String变量赋一个新值,都会创建一个新的String对象,很多有涉及到字符串本身的改变都是 ...

  8. UVA1363 - Joseph's Problem(数学,迷之优化)

    题意:给出n和k,1≤n,k≤1e9,计算 切入点是k/i 和 k/(i+1)差距不大.令pi = k/i, ri = k%i.如果pi+1 == pi,那么ri+1 == k - pi(i+1) = ...

  9. 【洛谷4287】[SHOI2011] 双倍回文(Manacher算法经典题)

    点此看题面 大致题意: 求一个字符串中有多少个长度为偶数的回文串,它的一半也是回文串. \(Manacher\)算法 这应该是\(Manacher\)算法一道比较好的入门题,强烈建议在做这题之前先去学 ...

  10. CUDA编程时,线程块的处理方法