码农知识堂 - 1000bd
  •   Python
  •   PHP
  •   JS/TS
  •   JAVA
  •   C/C++
  •   C#
  •   GO
  •   Kotlin
  •   Swift
  • 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;
        }
    };
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15

    结果:

    超时了。

    复杂度分析:

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

    空间复杂度:O(1)。

    解法2:哈希

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

    结果报错了:

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

    用 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.cn id=1128 lang=cpp
     *
     * [1128] 等价多米诺骨牌对的数量
     */
    
    // @lc code=start
    // class Solution
    // {
    // public:
    //     int numEquivDominoPairs(vector> &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(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) / 2
                count += freq * (freq - 1) / 2;
            }
            return count;
        }
    };
    // @lc code=end
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46

    结果:

    在这里插入图片描述

    复杂度分析:

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

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

  • 相关阅读:
    Linux系统调优详解(十二)——IO调优之磁盘测速
    Docker和Docker compose的安装使用指南
    2 OpenCV实现的F矩阵+RANSAC原理与实践
    如何用Python搭建监控平台
    人工智能轨道交通行业周刊-第10期(2022.8.15-8.21)
    c#中容易被忽视的foreach
    【离散化】【 树状树状 】100246 将元素分配到两个数组中
    Python爬虫入门学习:拿捏高级数据类型
    MySQL系统与内建函数
    管理器注册类方法和调用类函数方法
  • 原文地址:https://blog.csdn.net/ProgramNovice/article/details/134211571
  • 最新文章
  • 攻防演习之三天拿下官网站群
    数据安全治理学习——前期安全规划和安全管理体系建设
    企业安全 | 企业内一次钓鱼演练准备过程
    内网渗透测试 | Kerberos协议及其部分攻击手法
    0day的产生 | 不懂代码的"代码审计"
    安装scrcpy-client模块av模块异常,环境问题解决方案
    leetcode hot100【LeetCode 279. 完全平方数】java实现
    OpenWrt下安装Mosquitto
    AnatoMask论文汇总
    【AI日记】24.11.01 LangChain、openai api和github copilot
  • 热门文章
  • 十款代码表白小特效 一个比一个浪漫 赶紧收藏起来吧!!!
    奉劝各位学弟学妹们,该打造你的技术影响力了!
    五年了,我在 CSDN 的两个一百万。
    Java俄罗斯方块,老程序员花了一个周末,连接中学年代!
    面试官都震惊,你这网络基础可以啊!
    你真的会用百度吗?我不信 — 那些不为人知的搜索引擎语法
    心情不好的时候,用 Python 画棵樱花树送给自己吧
    通宵一晚做出来的一款类似CS的第一人称射击游戏Demo!原来做游戏也不是很难,连憨憨学妹都学会了!
    13 万字 C 语言从入门到精通保姆级教程2021 年版
    10行代码集2000张美女图,Python爬虫120例,再上征途
Copyright © 2022 侵权请联系2656653265@qq.com    京ICP备2022015340号-1
正则表达式工具 cron表达式工具 密码生成工具

京公网安备 11010502049817号