Leetcode1128. 等价多米诺骨牌对的数量

编程入门 行业动态 更新时间:2024-10-22 07:26:58

Leetcode1128. 等价多米诺骨牌对的<a href=https://www.elefans.com/category/jswz/34/1770431.html style=数量"/>

Leetcode1128. 等价多米诺骨牌对的数量

Every day a Leetcode

题目来源:1128. 等价多米诺骨牌对的数量

解法1:暴力

代码:

class Solution
{
public:int numEquivDominoPairs(vector<vector<int>> &dominoes){int n = dominoes.size(), count = 0;for (int i = 0; i < n - 1; i++)for (int j = i + 1; j < n; j++){if (dominoes[i] == dominoes[j] || dominoes[i] == vector<int>(dominoes[j].rbegin(), dominoes[j].rend()))count++;}return count;}
};

结果:

超时了。

复杂度分析:

时间复杂度:O(n2),其中 n 是数组 dominoes 的长度。

空间复杂度:O(1)。

解法2:哈希

遍历数组 dominoes,对每一个多米诺骨牌 domino 排序,这样就保证等价的多米诺骨牌能存储在一个哈希值里。用一个形如 unordered_map<vector<int>, int> 的哈希表,记录等价的多米诺骨牌的数量。

结果报错了:

error: call to implicitly-deleted default constructor of ‘unordered_map<vector<int>, int>‘

用 map 就行了。

具体办法:error: call to implicitly-deleted default constructor of ‘unordered_map<vector<int>, int>‘

设一种多米诺骨牌的出现次数为 n,则等价的骨牌对 (i, j) 的数量为 C(n, 2) = n * (n - 1) / 2。答案为等价的骨牌对 (i, j) 的数量的总和。

代码:

/** @lc app=leetcode id=1128 lang=cpp** [1128] 等价多米诺骨牌对的数量*/// @lc code=start
// class Solution
// {
// public:
//     int numEquivDominoPairs(vector<vector<int>> &dominoes)
//     {
//         int n = dominoes.size(), count = 0;
//         for (int i = 0; i < n - 1; i++)
//             for (int j = i + 1; j < n; j++)
//             {
//                 if (dominoes[i] == dominoes[j] || dominoes[i] == vector<int>(dominoes[j].rbegin(), dominoes[j].rend()))
//                     count++;
//             }
//         return count;
//     }
// };class Solution
{
public:int numEquivDominoPairs(vector<vector<int>> &dominoes){int n = dominoes.size();map<vector<int>, int> hash;for (vector<int> &domino : dominoes){sort(domino.begin(), domino.end());hash[domino]++;}int count = 0;for (auto it = hash.begin(); it != hash.end(); it++){int freq = it->second;// 组合:C(n, 2) = n * (n - 1) / 2count += freq * (freq - 1) / 2;}return count;}
};
// @lc code=end

结果:

复杂度分析:

时间复杂度:O(n),其中 n 是数组 dominoes 的长度。

空间复杂度:O(n),其中 n 是数组 dominoes 的长度。

更多推荐

Leetcode1128. 等价多米诺骨牌对的数量

本文发布于:2023-11-17 03:54:10,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1637113.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:数量   多米诺骨牌

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!