2014-05-12 07:31

题目链接

原题:

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

代码:

  1. // http://www.careercup.com/question?id=24308662
  2. // nobody said the elements in both arrays are unique, why would bitset work?
  3. #include <algorithm>
  4. #include <iostream>
  5. #include <vector>
  6. using namespace std;
  7.  
  8. class Solution {
  9. public:
  10. void mergeIntersection(vector<int> &a1, vector<int> &a2, vector<int> &intersect) {
  11. sort(a1.begin(), a1.end());
  12. sort(a2.begin(), a2.end());
  13.  
  14. int i, j;
  15. int n1, n2;
  16.  
  17. i = ;
  18. j = ;
  19. n1 = (int)a1.size();
  20. n2 = (int)a2.size();
  21. intersect.clear();
  22. while (i < n1 && j < n2) {
  23. if (a1[i] < a2[j]) {
  24. ++i;
  25. } else if (a1[i] > a2[j]) {
  26. ++j;
  27. } else {
  28. intersect.push_back(a1[i]);
  29. ++i;
  30. ++j;
  31. }
  32. }
  33. };
  34. };
  35.  
  36. int main()
  37. {
  38. int n1, n2, n;
  39. vector<int> a1, a2;
  40. vector<int> intersect;
  41. int i;
  42. Solution sol;
  43.  
  44. while (cin >> n1 >> n2 && (n1 > && n2 > )) {
  45. a1.resize(n1);
  46. a2.resize(n2);
  47. for (i = ; i < n1; ++i) {
  48. cin >> a1[i];
  49. }
  50. for (i = ; i < n2; ++i) {
  51. cin >> a2[i];
  52. }
  53. sol.mergeIntersection(a1, a2, intersect);
  54.  
  55. cout << '{';
  56. n = (int)intersect.size();
  57. for (i = ; i < n; ++i) {
  58. i ? (cout << ' '), : ;
  59. cout << intersect[i];
  60. }
  61. cout << '}' << endl;
  62. }
  63.  
  64. return ;
  65. }

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

代码:

  1. // http://www.careercup.com/question?id=24308662
  2. // nobody said the elements in both arrays are unique, why would bitset work?
  3. #include <algorithm>
  4. #include <iostream>
  5. #include <vector>
  6. #include <unordered_map>
  7. using namespace std;
  8.  
  9. class Solution {
  10. public:
  11. void mergeIntersection(vector<int> &a1, vector<int> &a2, vector<int> &intersect) {
  12. if (a1.size() > a2.size()) {
  13. mergeIntersection(a2, a1, intersect);
  14. return;
  15. }
  16. unordered_map<int, pair<int, int> > um;
  17. int n1, n2;
  18. int i;
  19.  
  20. n1 = (int)a1.size();
  21. n2 = (int)a2.size();
  22. unordered_map<int, pair<int, int> >::iterator it;
  23.  
  24. for (i = ; i < n1; ++i) {
  25. it = um.find(a1[i]);
  26. if (it == um.end()) {
  27. um[a1[i]] = make_pair(, );
  28. } else {
  29. ++it->second.first;
  30. }
  31. }
  32.  
  33. for (i = ; i < n2; ++i) {
  34. it = um.find(a2[i]);
  35. if (it != um.end()) {
  36. ++it->second.second;
  37. }
  38. }
  39.  
  40. intersect.clear();
  41. for (it = um.begin(); it != um.end(); ++it) {
  42. n1 = min(it->second.first, it->second.second);
  43. for (i = ; i < n1; ++i) {
  44. intersect.push_back(it->first);
  45. }
  46. }
  47.  
  48. um.clear();
  49. };
  50. };
  51.  
  52. int main()
  53. {
  54. int n1, n2, n;
  55. vector<int> a1, a2;
  56. vector<int> intersect;
  57. int i;
  58. Solution sol;
  59.  
  60. while (cin >> n1 >> n2 && (n1 > && n2 > )) {
  61. a1.resize(n1);
  62. a2.resize(n2);
  63. for (i = ; i < n1; ++i) {
  64. cin >> a1[i];
  65. }
  66. for (i = ; i < n2; ++i) {
  67. cin >> a2[i];
  68. }
  69. sol.mergeIntersection(a1, a2, intersect);
  70.  
  71. cout << '{';
  72. n = (int)intersect.size();
  73. for (i = ; i < n; ++i) {
  74. i ? (cout << ' '), : ;
  75. cout << intersect[i];
  76. }
  77. cout << '}' << endl;
  78. }
  79.  
  80. return ;
  81. }

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. linux 命令——14 head (转)

    head 与 tail 就像它的名字一样的浅显易懂,它是用来显示开头或结尾某个数量的文字区块,head 用来显示档案的开头至标准输出中,而 tail 想当然尔就是看档案的结尾. 1.命令格式: hea ...

  2. webpack最简单的入门教程里bundle.js之运行单步调试的原理解析

    读这篇文章的朋友,请确保对webpack有最基础的认识. 您可以阅读我前一篇文章:Webpack 10分钟入门 来在本地运行一个Webpack的hello world项目.https://www.to ...

  3. Ruby 学习笔记(一)

    环境搭建 本文基于Mac OS,windowns坑较多,建议使用Mac. xcode-select -p 检查是否安装xcode-select, 如果没有,通过xcode-select --insta ...

  4. ES6中set和map的区别

    Set ES6提供了新的数据结构Set.它类似于数组,但是成员的值都是唯一的,没有重复的值. Set函数可以接受一个数组(或类似数组的对象)作为参数,用来初始化. // 例一 var set = ne ...

  5. python时间转换 ticks-FYI

    #设a为字符串 import time a = "2011-09-28 10:00:00" #中间过程,一般都需要将字符串转化为时间数组 time.strptime(a,'%Y-% ...

  6. hdu-1162 Eddy's picture---浮点数的MST

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1162 题目大意: 给n个点,求MST权值 解题思路: 直接prim算法 #include<bi ...

  7. POJ-3565 Ants---KM算法+slack优化

    题目链接: https://vjudge.net/problem/POJ-3565 题目大意: 在坐标系中有N只蚂蚁,N棵苹果树,给你蚂蚁和苹果树的坐标.让每只蚂蚁去一棵苹果树, 一棵苹果树对应一只蚂 ...

  8. python实现二叉树的镜像

    题目描述 操作给定的二叉树,将其变换为源二叉树的镜像. 输入描述: 二叉树的镜像定义:源二叉树 8 / \ 6 10 / \ / \ 5 7 9 11 镜像二叉树 8 / \ 10 6 / \ / \ ...

  9. java设计模式——单例模式(三)

    容器单例模式 之前学习Structs2,Spring框架时,经常会听到单例,多例.虽然这与单例模式不太一样,但是都很类似.在程序运行的时候,就加载所有的实例,然后用的时候直接取出 看下面代码: /** ...

  10. 面向对象编程 -------JavaScrip

    本文摘要:http://www.liaoxuefeng.com/ 一定明白面向对象的两个基本概念: 类:类是对象的类型模板,例如,定义Student类来表示学生,类本身是一种类型,Student表示学 ...