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.

其实就是找到序列中出现次数最多的元素,而且已经提出假设这个序列不为空,同时这样的数字是一定存在的

我自己的解法比较土,就是利用一个map记录了每一个元素的出现的次数

class Solution {
public:
    int majorityElement(vector<int> &num) {
        int length = num.size();
        map<int,int> NumCount;
        for(int i = 0; i < length; i++)
        {
            NumCount[num[i]]++;
        }
        int maxNum = 0;
        int RetVal = 0;
        map<int,int>::iterator mapit = NumCount.begin();
        for(;mapit != NumCount.end(); mapit++)
        {
            if(mapit->second > maxNum)
            {
                maxNum = mapit->second;
                RetVal = mapit->first;
            }
        }
        return RetVal;
    }
};

每找出两个不同的element,则成对删除。最终剩下的一定就是所求的。

可扩展到? n/k ?的情况,每k个不同的element进行成对删除。但是这个是在这样的数据一定存在的前提下才可以,如果有两个数出现的次数相同,这个就不好扩展了

class Solution {
public:
    int majorityElement(vector<int> &num) {
        int nTimes = 0;
        int candidate = 0;
        for(int i = 0; i < num.size(); i ++)
        {
            if(nTimes == 0)
            {
                candidate = num[i];
                nTimes = 1;
            }
            else
            {
                if(candidate == num[i])
                    nTimes ++;
                else
                    nTimes --;
            }
        }
        return candidate;
    }
};
class Solution {
public:
    int majorityElement(vector<int> &num) {
         int n = num.size();
        sort(num.begin(),num.end());
        return num[n/2];
    }
};
时间: 2024-10-01 11:07:53

LeetCode—Majority Element的相关文章

LeetCode &quot;Majority Element II&quot;

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]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——Majority Element

在一个数组中找到主要的元素,也就是出现次数大于数组长度一半的元素.容易想到的方式就是计数,出现次数最多的就是majority element,其次就是排序,中间的就是majority element.但是还有两种更有意思的实现方式时间效率O(n),空间效率O(1):1.Moore voting algorithm 投票算法,因为符合要求的majority element总是存在的,所以首先置计数器count=1,并选择数组的第一个元素作为candidate,往后遍历并计数,与candidate相

leetcode Majority Element python

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. python code

[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 @t

LeetCode:Majority Element Ⅱ

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. 解法一: 1 class Solution { 2 public: 3 vector<int> majorityElement(vector<int>& nums) { 4 unor

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 Problem: 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. 思路1:Moore voting algorith

【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个嘛.因此,接着上一题的方