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?Show More Hint

思路1:

题目中重点强调出现的次数大于⌊ n/3 ⌋,所以可能有0个、1个或者2个(最多2个)这样的数存在,所以只需要设置2个变量cand1,、cand2来记录出现次数可能大于⌊ n/3 ⌋的数据,分别用count1和count2记录数据出现的次数。

思路2:

另一种最直接的方法就是用map对数组中每个值出现的次数做记录,然后把出现次数大于⌊ n/3 ⌋的值存入返回结果,该方法的缺点是空间复杂度为O(n)。

代码1实现:

class Solution {
public:vector<int> majorityElement(vector<int>& nums) {vector<int> result;if(nums.size() < 1) return result;if(nums.size() == 1) return nums;int cand1 = 0, cand2 = 0;int count1 = 0, count2 = 0;//找到满足条件的数,可能有一个满足条件的,最多有两个满足条件的for(auto num: nums){if (count1 == 0)cand1 = num;else if (count2 == 0)cand2 = num;//处理count的值if(cand1 == num)++count1;else if(cand2 == num)++count2;else{--count1;--count2;}}if(count(nums.begin(), nums.end(), cand1) > nums.size() / 3)result.push_back(cand1);//此处cand1 != cand2一定要判断,否则对于数组[2,2],就会返回[2,2],而期望的结果是[2]if(cand1 != cand2 && count(nums.begin(), nums.end(), cand2) > nums.size() / 3)result.push_back(cand2);return result;}
};

代码实现2:

class Solution {
public:vector<int> majorityElement(vector<int>& nums) {map<int, int> myMap;for (auto& num: nums) myMap[num]++;vector<int> res;for (auto it = myMap.begin(); it != myMap.end(); it++) if (it->second > nums.size()/3)res.push_back((*it).first);return res;}
};

【Leet Code】229. Majority Element II---Medium相关推荐

  1. Leet Code OJ 169. Majority Element [Difficulty: Easy]

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

  2. LeetCode 229 : Majority Element II

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

  3. 229. Majority Element II 【M】【52】

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

  4. 229. Majority Element II**

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

  5. leetcode 229. Majority Element II(多数投票算法)

    就是简单的应用多数投票算法(Boyer–Moore majority vote algorithm),参见这道题的题解. class Solution { public:vector<int&g ...

  6. [leetcode] 229. Majority Element II

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

  7. 229. Majority Element II

    class Solution(object): def majorityElement(self, nums): """ :type nums: List[int] :r ...

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

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

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

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

最新文章

  1. iOS 减少编译时间
  2. rabbitMQ第一篇:rabbitMQ的安装和配置
  3. Spring Cloud Finchley版中Consul多实例注册的问题处理
  4. WinForm打印之页边距
  5. pyinstaller安装失败解决
  6. 一个人的旅行 图论最短路问题
  7. input失去焦点和获得焦点jquery焦点事件
  8. 软文诊断50期: 百度账号有什么推广功能?能编辑软文吗?
  9. 任天堂xci文件提取romfs
  10. 安捷伦mso8104a示波器电源烧毁故障维修【图文】
  11. 图解数据交换技术——电路交换、报文交换、分组交换
  12. Java中IO流详细整合(含案例)
  13. 世界各国首都经纬度-json
  14. PS 将签名背景修改为透明色
  15. 使用selenium爬取百合网
  16. 让IE窗口最小化最大化的快捷键
  17. iOS 调整图片尺寸和大小的两个方法
  18. C语言中for循环的一些坑!!!
  19. python时间索引_Python时间戳作为索引
  20. 攻克 Linux 系统编程

热门文章

  1. N9H30 FMI NAND controller driver for RTT
  2. manjaro kde安装 配置教程
  3. [3,3‘-联吡啶]-6,6‘-二甲醛 cas1264748-06-2 中间体材料
  4. 量化投资学习——多因子模型选股
  5. 一个好的软件,除了给我们带来效率,更重要的是为我们带来了快乐!
  6. 致铭主板好礼等着您拿
  7. GEE:内存超限?将研究区划分成规则的小块运算
  8. 不再追求安全感,你才能走向成熟。
  9. 遥感影像的“全色”和“多光谱”
  10. 高性能微信公众平台开发