【问题标题】:Check if two arrays are equal or not ?? eg: {1,2,3,4,5} == {4,5,3,2,1}检查两个数组是否相等??例如:{1,2,3,4,5} == {4,5,3,2,1}
【发布时间】:2020-09-16 02:37:00
【问题描述】:

这种方法有什么问题吗?还是很好?我们必须判断两个数组是否相等 就像它们中的数字一样,它们的频率必须相同,而与它们的顺序无关。

#include<bits/stdc++.h>
using namespace std;

main(){

int t,n,i,in;
vector<int> x;

cin>>t;
while(t--){

   unordered_map<int,int> a,b;
   cin>>n;

   for(i=0;i<n;i++){
       cin>>in;
       x.push_back(in);
       a[in]++;
   }

   for(i=0;i<n;i++){
       cin>>in;
       b[in]++;
   }

   for(i=0;i<n;i++){

       if(b.find(x[i]) == b.end()){
           cout<<"0"<<endl;
           goto x;
       }

       if(a[x[i]] != b[x[i]]){
           cout<<"0"<<endl;
           goto x;
       }
   }

   cout<<"1"<<endl;
   x : ;
   }
}

【问题讨论】:

  • 在 0 到 Chrome 的范围内,您真的可以使用无用的内存。而且你没有做 Chrome 的工作。
  • 我推荐learning from a good book。无论你从现在学到什么,都会让你走向失败。
  • 你为什么不用if (a == b)
  • 将数字读入两个向量,然后对两个向量进行排序,然后比较它们。

标签: c++ hash stl c++14 unordered-map


【解决方案1】:

我不知道您对 C++ 有多陌生,所以这里有一个可能的解决方案,并有一些解释。

#include <map>
#include <vector>
#include <iostream>

//Avoid using namespace std as it can cause problems with conflicts of names
//and other sorts of nasty issues
using std::cout;
using std::cin;
using std::map;
using std::vector;
using std::endl;

//By using an ordered map you can more easily compare them
//The arguments are passed as const (you will never modify them)
//and also as reference (&) since you don't need to copy them.
bool CheckIfEqual (const vector<int> & V1, const vector<int> & V2) {
    //If the vectors don't have the same size you can just return false
    if (V1.size() != V2.size()) {
        return false;
    }
    map <int, size_t> M1;
    map <int, size_t> M2;
    //This type of loop goes through all elements of the vector and 
    //either creates a corrisponding value in the map or, if it is
    //already present, it increases it to accout for repetitions.
    //Map is automatically sorted.
    for (auto & Elem : V1) {
        M1[Elem]++;
    }
    for (auto & Elem : V2) {
        M2[Elem]++;
    }
    return M1 == M2;
}

//The main function is used to provide some examples
int main () {
    //Expected output: true
    vector<int> V1 {1, 2, 3, 4, 5};
    vector<int> V2 {1, 2, 3, 4 ,5};
    cout << CheckIfEqual(V1, V2) << endl;

    //Expected output: true
    V1 = {1, 2, 3, 4, 5};
    V2 = {5, 3, 2, 1 ,4};
    cout << CheckIfEqual(V1, V2) << endl;

    //Expected output: false
    V1 =  {1, 2, 3};
    V2 =  {5, 3};
    cout << CheckIfEqual(V1, V2) << endl;

    //Expected output: false
    V1 =  {1, 2, 3, 4, 5};
    V2 = {5, 3, 2, 1 ,1};
    cout << CheckIfEqual(V1, V2) << endl;

    //Expected output: true
    V1 = {1, 5, 5, 4, 5};
    V2 = {5, 5, 5, 1 ,4};
    cout << CheckIfEqual(V1, V2) << endl;
}

输出是

1
1
0
0
1

在使用goto 时也请非常小心,它已被弃用,仅在您需要同时跳出许多嵌套循环时才有用。一个简单的break 会更好。

【讨论】:

  • 这可以使用散列来完成吗? O(n) 中的无序 _map ?
  • 如果我没记错的话,我的解决方案应该是 O(n),因为构建地图是 O(log(n)),而运算符“==”是 O(n)。
  • 构建地图是 O(NlogN),因为有 N 条目,每个条目都需要 O(logN) 才能插入。
  • 哦,谢谢指正,那我想我不得不说“我不知道”,以前从未使用过散列。
猜你喜欢
  • 2020-02-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-05-05
  • 2021-11-15
  • 1970-01-01
相关资源
最近更新 更多