Majority Element II

Given an integer array of size n, find all elements that appear more than ⌊ n/3 ⌋ times. The algorithm should run in linear time and in O(1) space.

Hint:

  1. How many majority elements could it possibly have?

跟出现超过一半的数差不多,出现超过1/3的数最多有2个,扫描一遍数组,分别计数数组中的元素,当出现3个不同的元素时,把这3个元素都抛弃掉,如果有符合要求的数,那么它一定在最后剩下的数中,再分别判断一下剩下的数就能得到答案。

 1 class Solution {
 2 public:
 3     vector<int> majorityElement(vector<int>& nums) {
 4         int k = 3;
 5         vector<int> val(k), cnt(k, 0);
 6         for (int i = 0; i < nums.size(); ++i) {
 7             bool flag = true;
 8             for (int j = 0; j < k; ++j)  {
 9                 if (val[j] == nums[i] && cnt[j] > 0) {
10                     val[j] = nums[i];
11                     ++cnt[j];
12                     flag = false;
13                     break;
14                 }
15             }
16             if (flag) {
17                 for (int j = 0; j < k; ++j) {
18                     if (cnt[j] == 0) {
19                         val[j] = nums[i];
20                         ++cnt[j];
21                         break;
22                     }
23                 }
24             }
25             if (cnt[k - 1] == 1) {
26                 for (int kk = 0; kk < k; ++kk) {
27                     --cnt[kk];
28                 }
29             }
30         }
31         vector<int> res;
32         for (int i = 0; i < k; ++i) if (cnt[i] > 0) {
33                 int c = 0;
34                 for (auto n : nums) if (val[i] == n) ++c;
35                 if (c > nums.size() / k) res.push_back(val[i]);
36         }
37         return res;
38     }
39 };

[LeetCode] Majority Element II相关推荐

  1. LeetCode Majority Element II(Moore Voting Algorithm即Majority Voting Algorithm)

     Given an integer array of size n, find all elements that appear more than ⌊ n/3 ⌋ times. The algo ...

  2. [leetcode]Majority Element II

    思路:可以使用三向切分的快速排序算法,因为这种算法会把小于某个数,等于某个数,和大于某个数的分开,分成三部分.很容易判断等于某个数的全部数量,对于这种重复值问题,很有效. public class S ...

  3. Majority Element(169) Majority Element II(229)

    寻找多数元素这一问题主要运用了:Majority Vote Alogrithm(最大投票算法) 1.Majority Element 1)description Given an array of s ...

  4. [Leetcode] Majority Element 众数

    Majority Element I Given an array of size n, find the majority element. The majority element is the ...

  5. LeetCode 229 : Majority Element II

    Given an integer array of size n, find all elements that appear more than ⌊ n/3 ⌋ times. The algorit ...

  6. LeetCode Majority Element

    Given an array of size n, find the majority element. The majority element is the element that appear ...

  7. [LeetCode] Majority Element

    Given an array of size n, find the majority element. The majority element is the element that appear ...

  8. [LeetCode]Majority Element

    题目描述: Given an array of size n, find the majority element. The majority element is the element that ...

  9. leetcode 229. Majority Element II | 229. 求众数 II(找出现次数超过n/k的元素)

    题目 https://leetcode.com/problems/majority-element-ii/ 题解 思路来源于左程云<程序员代码面试指南> 问题描述 原问题:给定一个整型数组 ...

最新文章

  1. 面试:URI中的 “//” 有什么用?
  2. [公测]微信小程序设计指南文档
  3. idea 构建spring_以Spring方式构建企业Java应用程序
  4. Bootstrap列表组的情景类
  5. python实现小型搜索引擎设计_推荐:为图像处理初学者设计的100个问题(含python实现代码)...
  6. 一个简单的倒计时js插件
  7. html模块常用命名总结
  8. 史上最全的微信小程序代码大全
  9. pycharm下的xlwings+VBA混合编程注意事项
  10. 游戏贪吃蛇计分c语言,C语言实现贪吃蛇游戏(命令行)
  11. 仿真proteus8.7安装
  12. 数据结构作业总结_三种迷宫生成算法+三种走迷宫算法+pyqt5可视化(1)
  13. android sqlite同时读写,SQLite同时读写
  14. matlab特征值分解
  15. matplotlib使用笔记
  16. 研究生综合英语unit4 前四段
  17. 群贤路附近哪有计算机学校,群贤路口附近展览馆、会展中心
  18. ​PDF怎么转换成Word?试试这几种好用的转换方法
  19. Linux网络操作系统期末系统复习题
  20. osi七层模型_4、OSI七层模型简介

热门文章

  1. request failed with status code 500翻译_英语歌词的文言文翻译:文艺清新还是矫揉造作?...
  2. 5 高可靠,构建RabbitMQ集群架构
  3. yum install -y iptraf 显示找不到命令
  4. PHP中4个包含文件方法的差异
  5. 鸿蒙手表怎么安装,万物皆可鸿蒙,这次鸿蒙系统将应用到手表上了
  6. SpringBoot 精通系列-使用Swagger2构建RESTful APIs
  7. Leetcode-二叉搜索树最近公共祖先
  8. Maven 每次update之后自动修改jdk问题
  9. Windows 64位下安装Redis超详细教程
  10. pcb板子制作各层的解释和用法