0
点赞
收藏
分享

微信扫一扫

LeetCode_Array_229. Majority Element II 求众数 II(C++/Java)摩尔投票法


目录

​​一,题目描述​​

​​英文描述​​

​​中文描述​​

​​二,解题思路​​

​​1,摩尔投票法​​

​​1.1 适用场景​​

​​1.2 基本思想​​

​​1.3 算法步骤​​

​​1.4 简单示例​​

​​2,解题思路​​

​​三,AC代码​​

​​C++​​

​​Java​​

​​四,解题过程​​

​​第一博​​

一,题目描述

原题链接​​229. 求众数 II​​

英文描述

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

Follow-up: Could you solve the problem in linear time and in O(1) space?

 

Example 1:

Input: nums = [3,2,3]
Output: [3]
Example 2:

Input: nums = [1]
Output: [1]
Example 3:

Input: nums = [1,2]
Output: [1,2]
 

Constraints:

1 <= nums.length <= 5 * 104
-109 <= nums[i] <= 109

中文描述

给定一个大小为 n 的数组,找出其中所有出现超过 ⌊ n/3 ⌋ 次的元素。

进阶:尝试设计时间复杂度为 O(n)、空间复杂度为 O(1)的算法解决此问题。

 

示例 1:

输入:[3,2,3]
输出:[3]
示例 2:

输入:nums = [1]
输出:[1]
示例 3:

输入:[1,1,1,3,3,2,2,2]
输出:[1,2]
 

提示:

1 <= nums.length <= 5 * 10^4
-10^9 <= nums[i] <= 10^9


链接:https://leetcode-cn.com/problems/majority-element-ii
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

二,解题思路

摩尔投票法参考​​@我脱下短袖【两幅动画演示摩尔投票法,最直观的理解方式】​​。动画展示清晰易懂!

由于解题需要,这里再简单介绍下摩尔投票法:

1,摩尔投票法

1.1 适用场景

求一个数组的一个众数,众数满足:出现次数>n / 2;

求一个数组的两个众数,众数满足:出现次数>n / 3;

求一个数组的三个众数,众数满足:出现次数>n / 4;

1.2 基本思想

采用投票抵消的方法。

cand记录当前候选人,count记录候选人当前票数,遍历数组:

当前元素为候选人时,票数加一;

否则判断该候选人的剩余票数:

  • 若当前票数为0时,更换候选人为当前元素,且重置count为1;
  • 否则count减一;

由于符合条件的候选者出现次数>n/2,即获得了一半以上的选票,所以这样一一抵消之后,至少还会剩余一票,即最后count>0对应的cand有很大几率成为最终答案(如果最终答案不是cand,那么说明数组中不存在符合条件的元素);

之所以说是很大几率,是因为如果数组中不存在符合条件的元素时,遍历到数组最后,count也可能大于1,比如[1,2,3,4,5,6,7,7],因而为了确定最终答案,还需要再次遍历一次数组,确定cand是否为最终答案;

1.3 算法步骤

抵消阶段:两个不同投票进行对坑,并且同时抵消掉各一张票,如果两个投票相同,则累加可抵消的次数;

计数阶段:在抵消阶段最后得到的抵消计数只要不为 0,那这个候选人是有可能超过一半的票数的,为了验证,则需要遍历一次,统计票数,才可确定。

1.4 简单示例

以数组[A, B, C, A, A, B, A]为例,寻找出现次数> n/2的元素:

初始化        A              B             C             A             A              B             A

[A:0] :[A:1] -》[A:0] -》[C:1] -》[C:0] -》[A:1] -》[A:0] -》[A:1]

2,解题思路

由于本题中需要找出所有出现次数> n / 3的元素,可知这样的元素最多有两个,所以设计cand1、cand2记录两个候选人,count1、count2记录两位候选人的票数;

1)投票阶段:

当前元素为候选人之一,该候选人票数加一,另一候选人票数不变;

否则,判断两位候选人票数是否都大于0:

  • 若是,则票数均减一;
  • 否则替换其中一位票数为0 的候选人为当前元素,重置其票数为1;

2)计数阶段:

再次遍历数组,判断两位候选人的票数是否符合条件;

三,AC代码

C++

class Solution {
public:
vector<int> majorityElement(vector<int>& nums) {
vector<int> ans;
if(nums.size() == 0) return ans;
int cand1 = nums[0], cand2 = nums[0]; // 候选人
int count1 = 0, count2 = 0; // 票数
// 投票阶段
for(int i = 0; i < nums.size(); i++) {
if(nums[i] == cand1 || nums[i] == cand2) { // 当前选票为候选人之一
nums[i] == cand1 ? count1++ : count2++;
}else {
if(count1 != 0 && count2 != 0) { // 候选人票数均大于0时 票数各减1
count1--;
count2--;
} else {
// 其中一个或多个候选人票数为0 需要替换候选人 且只需替换掉一位即可
if(count1 == 0) {
cand1 = nums[i];
count1 = 1;
}else {
cand2 = nums[i];
count2 = 1;
}
}
}
}
// 计数阶段
count1 = count2 = 0; // 票数清0
for(int i : nums) {
if(cand1 == i) count1++; // 这里使用if-else语句 保证当cand1==cand2时 票数只加在一个候选人上
else if(cand2 == i) count2++;
}
if(count1 > nums.size() / 3) ans.push_back(cand1);
if(count2 > nums.size() / 3) ans.push_back(cand2);
return ans;
}
};

Java

class Solution {
public List<Integer> majorityElement(int[] nums) {
List<Integer> ans = new ArrayList<>();
if(nums.length == 0) return ans;
int cand1 = nums[0], cand2 = nums[0];
int count1 = 0, count2 = 0;
// 投票
for(int i = 0; i < nums.length; i++) {
if(nums[i] == cand1 || nums[i] == cand2) {
if(nums[i] == cand1) count1++;
else count2++;
} else {
if(count1 > 0 && count2 > 0) {
count1--;
count2--;
} else {
if(count1 == 0) {
cand1 = nums[i];
count1 = 1;
} else {
cand2 = nums[i];
count2 = 1;
}
}
}
}
// 计数
count1 = count2 = 0;
for(int k : nums) {
if(k == cand1) count1++;
else if(k == cand2) count2++;
}
if(count1 > nums.length / 3) ans.add(cand1);
if(count2 > nums.length / 3) ans.add(cand2);
return ans;
}
}

四,解题过程

第一博

思考:

  • 寻找数目大于n/3,也就是说答案数组中最多有两个元素;
  • 时间要求O(N),空间要求O(1)。也就意味着有限常数次遍历数组,且借助的空间只有几个变量;

没有想到好的方法,只好参考大佬给出的解法:摩尔投票法。妙不可言!

LeetCode_Array_229. Majority Element II 求众数 II(C++/Java)摩尔投票法_求众数

使用了4个int型变量,一个vector<int>数组存放结果,但是内存消耗有点出乎意料╮(╯▽╰)╭

举报

相关推荐

0 条评论