zoukankan      html  css  js  c++  java
  • Careercup

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

    代码:

     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 = 0;
    18         j = 0;
    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 > 0 && n2 > 0)) {
    45         a1.resize(n1);
    46         a2.resize(n2);
    47         for (i = 0; i < n1; ++i) {
    48             cin >> a1[i];
    49         }
    50         for (i = 0; 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 = 0; i < n; ++i) {
    58             i ? (cout << ' '), 1 : 1;
    59             cout << intersect[i];
    60         }
    61         cout << '}' << endl;
    62     }
    63     
    64     return 0;
    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 = 0; i < n1; ++i) {
    25             it = um.find(a1[i]);
    26             if (it == um.end()) {
    27                 um[a1[i]] = make_pair(1, 0);
    28             } else {
    29                 ++it->second.first;
    30             }
    31         }
    32         
    33         for (i = 0; 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 = 0; 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 > 0 && n2 > 0)) {
    61         a1.resize(n1);
    62         a2.resize(n2);
    63         for (i = 0; i < n1; ++i) {
    64             cin >> a1[i];
    65         }
    66         for (i = 0; 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 = 0; i < n; ++i) {
    74             i ? (cout << ' '), 1 : 1;
    75             cout << intersect[i];
    76         }
    77         cout << '}' << endl;
    78     }
    79     
    80     return 0;
    81 }
  • 相关阅读:
    [BZOJ4876][ZJOI2017]线段树
    [FJOI2016]建筑师(斯特林数)
    WC2018伪题解
    [BZOJ3514]CodeChef MARCH14 GERALD07加强版(LCT+主席树)
    [BZOJ2594][WC2006]水管局长加强版(LCT+Kruskal)
    [洛谷3796]【模板】AC自动机(加强版)
    [洛谷3808]【模板】AC自动机(简单版)
    [BZOJ3261]最大异或和
    [BZOJ3439]Kpm的MC密码
    [POI2006]Periods of Words
  • 原文地址:https://www.cnblogs.com/zhuli19901106/p/3722702.html
Copyright © 2011-2022 走看看