Careercup - Microsoft面试题 - 24308662
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的更多相关文章
- Careercup - Microsoft面试题 - 6314866323226624
2014-05-11 05:29 题目链接 原题: Design remote controller for me. 题目:设计一个遥控器. 解法:遥控什么?什么遥控?传统的红外线信号吗?我只能随便说 ...
- Careercup - Microsoft面试题 - 6366101810184192
2014-05-10 22:30 题目链接 原题: Design database locks to allow r/w concurrency and data consistency. 题目:设计 ...
- Careercup - Microsoft面试题 - 5700293077499904
2014-05-12 00:02 题目链接 原题: For a given map (ie Bing map) given longitude/latitude/ how would you desi ...
- Careercup - Microsoft面试题 - 5204967652589568
2014-05-11 23:57 题目链接 原题: identical balls. one ball measurements ........ dead easy. 题目:9个看起来一样的球,其中 ...
- Careercup - Microsoft面试题 - 5175246478901248
2014-05-11 23:52 题目链接 原题: design an alarm clock for a deaf person. 题目:为聋人设计闹钟? 解法:聋人听不见,那么闪光.震动都可行.睡 ...
- Careercup - Microsoft面试题 - 5718181884723200
2014-05-11 05:55 题目链接 原题: difference between thread and process. 题目:请描述进程和线程的区别. 解法:操作系统理论题.标准答案在恐龙书 ...
- Careercup - Microsoft面试题 - 5173689888800768
2014-05-11 05:21 题目链接 原题: Complexity of a function: int func_fibonacci ( int n) { ) { return n; } el ...
- Careercup - Microsoft面试题 - 6282862240202752
2014-05-11 03:56 题目链接 原题: Given an integer array. Perform circular right shift by n. Give the best s ...
- Careercup - Microsoft面试题 - 5428361417457664
2014-05-11 03:37 题目链接 原题: You have three jars filled with candies. One jar is filled with banana can ...
随机推荐
- Vue打包后页面出现cannot get
学习Vue有大半个月了,然而遇到了不少坑,完全没有高手们那么容易,中间有不少值得记录下的东东,回头好好理理.先理下今天的: Vue打包命令简单啊,直接在命令行输入:npm run build 然而没一 ...
- >>我要到处浪系列 之 JS随便投票小脚本
首先郑重声明:我不是对任何网站或者任何个人或组织有意见,仅仅是觉得 4点几 的评分对某些玩票的片段都太高了,为了落实想法,切实履行公民的投票权,并且 bibibabibobi biubiubiu..所 ...
- JAXB介绍一
参考博客: https://www.cnblogs.com/chenbenbuyi/p/8283657.html https://www.cnblogs.com/cnsdhzzl/p/8390514. ...
- COGS 1043. [Clover S2] Freda的迷宫
★ 输入文件:mazea.in 输出文件:mazea.out 简单对比时间限制:1 s 内存限制:128 MB Freda 的迷宫 (mazea.pas/.c/.cpp) 题目叙述 F ...
- php之cURL惯用
1.php cURL的强大:PHP 支持 Daniel Stenberg 创建的 libcurl 库,能够连接通讯各种服务器.使用各种协议.libcurl 目前支持的协议有 http.https.ft ...
- POJ 3181 Dollar Dayz(递推,两个long long)
题意:John有N美元,有价格为1~K的工具,可以买的个数不限,问1~K组合出N的方案数. f[i = 第i中工具][j = 花费为j] = 方案数. f[i][j] = sigma{ f[i-1][ ...
- 【BZOJ1076】[SCOI2008] 奖励关(状压DP)
点此看题面 大致题意:总共有\(n\)个宝物和\(k\)个回合,每个回合系统将随机抛出一个宝物(抛出每个宝物的概率皆为\(1/n\)),吃掉一个宝物可以获得一定的积分(积分可能为负),而吃掉某个宝物有 ...
- Struts2 In Action笔记_页面到动作的数据流入和流出
因为回答百度知道的一个问题,仔细查看了<Struts2 In Action>,深入细致的看了 “数据转移OGNL 和 构建视图-标签”,很多东西才恍然大悟. 一直觉得国外写的书很浮,不具有 ...
- Java源码——HashMap的源码分析及原理学习记录
学习HashMap时,需要带着这几个问题去,会有很大的收获: 一.什么是哈希表 二.HashMap实现原理 三.为何HashMap的数组长度一定是2的次幂? 四.重写equals方法需同时重写hash ...
- 工具之UltraEdit之正则表达式