[leetcode] 169. Majority Element

原创 2016年05月30日 11:00:32

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.

Solution:

Idea: sort numbers and return (n/2)th element

class Solution {
public:
    int majorityElement(vector<int>& nums) {
        sort(nums.begin(), nums.end());
        return nums[floor(nums.size()/2)];
    }
};

Solution: 

Idea: randomly select one element and check its appliance time in the array. It runs faster.

<span style="font-size:14px;">class Solution {
public:
    int majorityElement(vector<int>& nums) {
        srand(unsigned(time(NULL)));
        int n = nums.size();
        while(true){
            int elem = nums[rand()%n];
            int counter = 0;
            for(int i=0; i<n; i++){
                if(nums[i]==elem)
                    counter++;
            }
            if (counter > n/2)
                return elem;
        }

    }
};</span>

Solution:

Idea: hash table by using unordered_map

<span style="font-size:14px;">class Solution {
public:
    int majorityElement(vector<int>& nums) {
        unordered_map<int, int> counter;
        int n = nums.size();
        int major;
        for(int i=0; i<n; i++){
            if(++counter[nums[i]]>n/2){
                major = nums[i];
                break;
            }
        }
        return major;
    }
};</span>





版权声明:本文为博主原创文章,未经博主允许不得转载。

相关文章推荐

【LeetCode】169. Majority Element (多数投票算法 & 算法迁移能力)

Question1Given an array of size n, find the majority element. The majority element is the element th...

[leetcode] 169.Majority Element

题目: Given an array of size n, find the majority element. The majority element is the element that a...

leetcode[169]:Majority Element

Majority Element Given an array of size n, find the majority element. The majority element is the e...

【LEETCODE】169-Majority Element

Given an array of size n, find the majority element. The majority element is the element that appear...

LeetCode 169. Majority Element(众数)

原题网址:https://leetcode.com/problems/majority-element/ Given an array of size n, find the majority e...

【leetcode】Array—— Majority Element(169)

题目: 思路1:sort  很简单,把nums排序后,直接取下彪为num.length/2的值 代码:public int majorityElement1(int[] nums) { Arr...

Leetcode 169. Majority Element[easy]

GRE回归,响应老师号召,做分治题目。题目: Given an array of size n, find the majority element. The majority element is...

Leetcode 169 - Majority Element(Moore投票算法)

题意给一个数组,求它的majority,对majority的定义为:出现次数超过⌊n2⌋\lfloor \frac{n}{2} \rfloor的元素。思路算法1O(nlogn)O(nlogn)时间。排...
  • Lzedo
  • Lzedo
  • 2017-02-15 22:03
  • 115

【leetcode】169. Majority Element

一、题目描述 Given an array of size n, find the majority element. The majority element is the element...

LeetCode 169. Majority Element

课上系统的学习了算法分治的思想,于是想上leetcode找一找关于分治的题目做一做。             一开始选的是 312 Burst balloon, 难度为hard。想了一种贪心的做法,...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

(最多只允许输入30个字)