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 @ts for adding this problem and creating all test cases.

int majorityElement(vector &num) { }

题目大意

给定size 为n的数组,查找出主元素,就是出现次数大于n/2次的元素。你可以假定数组非空,而且主元素一定存在。

分析

首先肯定,这样的主元素只有一个,因为出现次数大于n/2。如何找到它呢?很暴力的做法是,两轮循环,然后把他找出来。时间复杂度是 O(n*n),这显然不是很好的方法。下面是我的一种解法:

int majorityElement(vector<int> &num) {
    std::map<int, int> im;
    for (int i = 0; i < num.size(); ++i){
        map<int, int>::iterator it = im.find(num[i]);
        if (it == im.end()) {
            im[num[i]] = 1;
        } else {
            im[num[i]]++;
        }
        if (im[num[i]] > num.size()/2) {
            return num[i];
        }
    }
    return 0;
}

这种解法还不是最好的,就这问题而已,有一种算法叫 Moore’s Voting Algorithm,由Robert S.Boyer 和J Strother Moore于1980年发明,是线性时间复杂度。

int majorityElement(vector<int> &num) {
    int majority;
    int cnt = 0;
    for(int i=0; i<num.size(); i++){
        if ( cnt ==0 ){
            majority = num[i];
            cnt++;
        }else{
            majority == num[i] ? cnt++ : cnt --;
            if (cnt >= num.size()/2+1) return majority;
        }
    }
    return majority;
}

当然,这种算法对于存在主元素的数组是有效的,如:

A A A C C B B C C C B C C

它肯定能返回主元素C。但是,如果不存在主元素,那么得到的结果就跟遍历顺序有关了。如:

A A A C C C B

如果是从左到右,那么结果是B,如果是从右到左,那么结果是A。

以上。

时间: 2024-10-06 04:11:40

LeetCode169——Majority Element的相关文章

leetcode169——Majority Element (C++)

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. 个人博客:http://www.cnblogs.com/

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 @

[LeetCode169]Majority Element求一个数组中出现次数大于n/2的数

题目: 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. 思路:可以利用Dictionary将数组中每个数

LeetCode(7): Majority Element

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. 题意:找出给定数组中的

[LeetCode]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 @

leetcode[169] Majority Element

在一个数组中找到主要的元素,也就是出现次数大于数组长度一半的元素. 我想到的方法是 1. 排序,然后扫描一次就知道了.总共nlgn 2. 哈希,记录每个次数,O(n)的时间和空间. class Solution { public: int majorityElement(vector<int> &num) { unordered_map<int, int> umap; for (int i = 0; i < num.size(); i++) { umap[num[i]

leetcode 169. Majority Element 多数投票算法(Boyer-Moore Majority Vote algorithm)

题目: 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. 题解:运用多数投票算法的思路来解:从头到尾遍历数

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. 哈希实现 通过map计数即可 1 class Solut

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. 求主元素:包含n个元素的数组中,如果一个元素的出现次数大