多数投票算法 --- A linear time majority vote algorithm
来源:互联网 发布:福彩快3源码 编辑:程序博客网 时间:2024/05/29 04:38
算法文档原文:
A Linear Time Majority Vote Algorithm
This algorithm, which Bob Boyer and I invented in 1980 decides which element of a sequence is in the majority, provided there is such an element. How would you determine the majority element of:sequence: A A A C C B B C C C B C C
You could count the number of occurrences of each element. Here is how we do it, in one pass.For details, see
A A A C C B B C C C B C C
We will sweep down the sequence starting at the pointer position shown above.
As we sweep we maintain a pair consisting of a current candidate and a counter. Initially, the current candidate is unknown and the counter is 0.
When we move the pointer forward over an element e:
- If the counter is 0, we set the current candidate to e and we set the counter to 1.
- If the counter is not 0, we increment or decrement the counter according to whether e is the current candidate.
Click on Step below to carry out the algorithm.
中文算法解释:
算法思路:一旦两个相邻的元素不同,酒吧这两个元素对冲抵消掉。由于众数的出现频次大于数据其他元素出现频率次之和,所以这种抵消最后剩下的一定是众数
代码块:
for i in xrange(L): if count == 0: candidate = nums[i] count += 1 else: if nums[i] == candidate: count += 1 else: count -= 1
0 0
- 多数投票算法 --- A linear time majority vote algorithm
- 多数投票算法 Majority Vote Algorithm
- 多数投票算法(Majority Vote Algorithm)
- 多数投票算法 Majority Vote Algorithm
- 多数投票算法 Majority Vote Algorithm
- Majority Vote Alogrithm 多数投票算法
- Boyer–Moore majority vote algorithm 博耶-穆尔多数投票算法 leetcode第229题
- 摩尔投票算法(Boyer–Moore majority vote algorithm)
- Majority Element浅谈 (A linear time iceberg query algorithm)
- 多数算法(Majority Algorithm)
- LeetCode--Majority Element II & 多数投票算法
- LeetCode--Majority Element II & 多数投票算法
- 多数投票算法(Boyer-Moore Algorithm)详解
- Moore's majority vote algorithm
- 【LeetCode】169. Majority Element (多数投票算法 & 算法迁移能力)
- The Boyer-Moore Majority Vote Algorithm
- Boyer–Moore majority vote algorithm
- Identifying Hierarchical Structure in Sequences: A linear-time algorithm 1997
- Android Studio 增加函数注释模板
- mysql基础知识
- 【python爬虫】爬取知乎收藏夹内所有图片
- 【4】构建基于scikit-learn的文本挖掘学习系统
- 实现抽象类内部的抽象方法
- 多数投票算法 --- A linear time majority vote algorithm
- SpringMVC通过@Value注解读取.properties配置
- decodeByteArray返回null
- 栈实验之数组逆序
- HDU 1525 Euclid's Game (博弈)
- Android5.0的更新包中system.new.dat文件的解包
- 50个C++学习网站
- SQLi Labs Lesson2
- js中setInterval与setTimeout用法