多数元素-哈希表

169. 多数元素 - 力扣(LeetCode)

哈希表来找出这个数出现几次

class Solution {
public:
    int majorityElement(vector<int>& nums) {
        unordered_map<int,int> counts;
        int cnt = 0, majority = 0;
        for(int num : nums){
            ++counts[num];
            if(counts[num] > cnt){
                majority = num;
                cnt = counts[num];
            }
        }
        return majority;
    }
};

相关推荐

  1. LeetCode:169.多数元素

    2024-07-14 11:28:05       54 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-07-14 11:28:05       67 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-07-14 11:28:05       72 阅读
  3. 在Django里面运行非项目文件

    2024-07-14 11:28:05       58 阅读
  4. Python语言-面向对象

    2024-07-14 11:28:05       69 阅读

热门阅读

  1. 简单理解跨域

    2024-07-14 11:28:05       38 阅读
  2. PHP MySQL 创建数据库

    2024-07-14 11:28:05       24 阅读
  3. 速盾:cdn加速端口映射?

    2024-07-14 11:28:05       17 阅读
  4. vue2上传文档例子,pdf转换word例子

    2024-07-14 11:28:05       16 阅读
  5. Makefiel技巧与分析

    2024-07-14 11:28:05       21 阅读
  6. 模板方法模式

    2024-07-14 11:28:05       16 阅读
  7. 《从零开始学习Linux》——开篇

    2024-07-14 11:28:05       24 阅读
  8. Python:逻辑运算符and比较运算符以及布尔输入

    2024-07-14 11:28:05       26 阅读
  9. C++ STL stable_sort用法

    2024-07-14 11:28:05       22 阅读
  10. Nikto 扫描 Web 服务器漏洞

    2024-07-14 11:28:05       24 阅读
  11. 优化实战篇—自关联的优化

    2024-07-14 11:28:05       18 阅读
  12. todolist-原生js(ES6)

    2024-07-14 11:28:05       22 阅读
  13. 日常学习--docker命令梳理--20240714

    2024-07-14 11:28:05       25 阅读
  14. iOS热门面试题(四)

    2024-07-14 11:28:05       26 阅读
  15. 56. 合并区间

    2024-07-14 11:28:05       23 阅读