算法(7)Majority Element II

题目:找出数组中出现次数大于n/3次的数字

思路:摩尔投票法。所有的帖子中都说:先遍历一遍数组找到备选元素,然后再遍历一遍数组考察下这个元素是否是真的超过n/3,然后就直接上代码,但是现在的问题是:我怎么找到这个备选的元素?!票是怎么投起来的呢?通过参考文章中的代码,大致明白了,醍醐灌顶(http://www.cnblogs.com/grandyang/p/4606822.html)。那么怎么遍历一遍数组然后找到备选元素呢?这个算法还得从找到大于一半的数说起:整个数组中有两个不一样的数的时候,就清除掉这两个一样的数,这样的话,最后一个数组里肯定会剩下一些数的!这就是投票的过程咯,然后遍历下数组,检查下这个被选出来的票就可以了!那么对应于n/3,n/4时,这样的情况会不会存在呢?对应于三个数的情况,数组中最多有2个数满足出现次数大于n/3,那么如果只有一个这样的数呢?比如[7,7,7,5,4,6,8,10]这样的呢?首先777入栈,如果也是单反目前栈里数字不一样的,那么这样两个数字就要出栈了,如此,那么当5,4,6袭来,7肯定就是pass掉了,这是大于n/2情况的分析,那么n/3情况的分析呢?应该是说,准备两个栈,两个栈中分别存着目前的两个候选人,然后每当挑战者来了之后,分别同这两个栈中的数字比较,如果不同,那么两个栈就各出局一个!【这里有个小细节,当两个栈中某个栈是空的,那么就挑战者就占用这个栈了】。所以,同理的话,当找到大于n/4的数字,我们就要准备3个栈了(因为最多也就3个元素),如果哪个栈是空的,挑战者就占着空闲的栈位,否则,如果挑战者不等于栈里的任何一个元素,栈里的元素都得弹出来呢!编程时,注意处理好这个”栈“!

答案

时间: 2024-10-10 07:06:27

算法(7)Majority Element II的相关文章

Majority Element && Majority Element II

Majority Element https://leetcode.com/problems/majority-element/ Difficulty: Easy 查找数组的多数元素(majority element) 多数元素为数组中出现次数多于?n/2?的元素.假设数组非空且多数元素一定存在 LeetCode的解答中给出了七种思路 第一种是Brute force solution,时间复杂度为O(n2),顾名思义,遍历数组,依次判断每个元素是否为多数元素 第二种是Hash table,时间复

169. Majority Element && 229. Majority Element II

169. Majority Element Given an array of size n, find the majority element. The majority element is the element that appears more than ⌊ n/2 ⌋ times. You may assume that the array is non-empty and the majority element always exist in the array. Hide T

【LeetCode】229. Majority Element II

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: How many majority elements could it possibly have? Do you have a better hint

[LeetCode][JavaScript]Majority Element II

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. https://leetcode.com/problems/majority-element-ii/ 使用Moore voting algorithm,时间复杂度是

LeetCode OJ 229. 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: How many majority elements could it possibly have? Do you have a better hint? Suggest it! [题目分析]

[LeetCode] 229. Majority Element II 多数元素 II

Given an integer array of size n, find all elements that appear more than ⌊ n/3 ⌋ times. Note: The algorithm should run in linear time and in O(1) space. Example 1: Input: [3,2,3] Output: [3] Example 2: Input: [1,1,1,3,3,2,2,2] Output: [1,2] 169. Maj

LeetCode "Majority Element II"

Similar as "Majority Element". There are at most 2 such elements, for > floor(n/3), and every non-hit element will decrease count of EACH hit element. https://leetcode.com/discuss/42769/o-n-time-and-in-o-1-space-c-solution

【LeetCode 229】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. 思路: [LeetCode 169]Majority Element 的拓展,这回要求的是出现次数超过三分之一次的数字咯,动动我们的大脑思考下,这样的数最多会存在几个呢,当然是2个嘛.因此,接着上一题的方

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

就是简单的应用多数投票算法(Boyer–Moore majority vote algorithm),参见这道题的题解. class Solution { public: vector<int> majorityElement(vector<int>& nums) { int cnt1=0,cnt2=0,ans1=0,ans2=1; for(auto n:nums){ if(n==ans1){ cnt1++; } else if(n==ans2){ cnt2++; } el