Moore's voting algorithm

算法的基本思想

这个算法是解决这样一个问题:从一个数组中找出出现半数以上的元素。他的基本思想是:每次都找出一对不同的元素,从数组中删掉,直到数组为空或只有一种元素。 不难证明,如果存在元素e出现频率超过半数,那么数组中最后剩下的就只有e。

算法的实现

 1 int majorityElement(vector<int> &num)
 2 {
 3     int curIdx = 0, count = 1;
 4     for (int i = 1; i < num.size(); ++i)
 5     {
 6         num[i] == num[curIdx] ? ++count : --count;
 7         if (!count)
 8         {
 9             curIdx = i;
10             count = 1;
11         }
12     }
13
14     return num[curIdx];
15 }

Moore's voting algorithm

时间: 2024-08-05 08:27:35

Moore's voting algorithm的相关文章

LeetcodeOJ: Implement strStr() [ Boyer–Moore string search algorithm ]

1 class Solution { 2 public: 3 int strStr(char *haystack, char *needle) { 4 5 int i = 0 , skip[256]; 6 char *str = haystack, *substr = needle; 7 int len_src = strlen(str), len_sub = strlen(substr); 8 // preprocess 9 for (i = 0; i < 256; i++) 10 skip[

Task 待学习内容【Moore majority vote algorithm(摩尔投票算法)】

https://www.cnblogs.com/zhonghuasong/p/6536665.html 参见:https://my.oschina.net/u/2277632/blog/2873164 原文地址:https://www.cnblogs.com/leodaxin/p/11355590.html

Lintcode 166. 主元素

----------------------------------- Moore's voting algorithm算法:从一个集合中找出出现次数半数以上的元素,每次从集合中去掉一对不同的数,当剩下一个元素的时候(事实上只要满足一个元素出现过半就一定会剩下一个元素的)这个元素就是我们要找的数了. AC代码: public class Solution { /** * @param nums: a list of integers * @return: find a majority numb

leetcode_num169_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. class Solution { public: int

Lintcode: Majority Number 解题报告

Majority Number 原题链接:http://lintcode.com/en/problem/majority-number/# Given an array of integers, the majority number is the number that occurs more than half of the size of the array. Find it. Example For [1, 1, 1, 1, 2, 2, 2], return 1 Challenge O(

leetcode 169. Majority Element 求出现次数最多的数 --------- java

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. 给定一个数组,求其中权制最大的元素,(该元素出现超过了一

leetcode 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. 思路: Find k different element

LeetCode[Array]: 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. 这个题目可以利用"位操作"来实现: 假设每个数

LeetCode169——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. Credits: Special thanks to @