[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.

【分析】

思路是计算数组前一半出现元素的频率,如果数目大于⌊ n/2 ⌋就返回

【代码】

/*********************************
*   日期:2015-01-30
*   作者:SJF0115
*   题目: 169.Majority Element
*   网址:https://oj.leetcode.com/problems/majority-element/
*   结果:AC
*   来源:LeetCode
*   博客:
**********************************/
#include <iostream>
#include <vector>
using namespace std;

class Solution {
public:
    int majorityElement(vector<int> &num){
        int len = num.size();
        int size = len / 2 + 1;
        int count;
        // 统计前一半的元素个数
        for(int i = 0;i < size;++i){
            // 跳过重复元素
            while(i > 0 && num[i] == num[i-1]){
                ++i;
            }//while
            count = 0;
            for(int j = i;j < len;++j){
                if(num[j] == num[i]){
                    ++count;
                    if(count >= (len+1)/2){
                        return num[i];
                    }//if
                }//if
            }//for
        }//for
    }
};

int main(){
    Solution solution;
    vector<int> num = {8,8,7,7,7};
    int result = solution.majorityElement(num);
    // 输出
    cout<<result<<endl;
    return 0;
}

【分析二】

Every number in the vector votes for itself, the majority number gets the most votes.
Different number offsets the votes.

遇到不同的就相互抵销,遇到相同的就增加,当count = 0时,重新开始

【代码二】

class Solution {
public:
    int majorityElement(vector<int> &num){
        int vote = num[0];
        int count = 1;
        int size = num.size();
        //vote from the second number
        for(int i = 1;i < size;++i){
            if(count == 0){
                vote = num[i];
                count++;
            }//if
            else if(vote == num[i]){
                count++;
            }
            else{
                count--;
            }
        }//for
        return vote;
    }
};

【分析】

Majority Element肯定占据至少一半的元素,因此排序后中间元素一定是Majority Element

【代码】

class Solution {
public:
    int majorityElement(vector<int> &num){
        int size = num.size();
        // 只有一个元素
        if(size == 1){
            return num[0];
        }//if
        sort(num.begin(),num.end());
        return num[size / 2];
    }
};
时间: 2024-11-08 17:41:57

[LeetCode]169.Majority Element的相关文章

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从零单排】No.169 Majority Element(hashmap用法)

题目 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

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. 解题思路:摩尔投票法.这种投票法先将第一个数字假设为众数

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. 题目大意: 找出数组中超过一半的数.   C++实现代码

[LeetCode] 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. 解题思路 摩尔投票法.投票法的核心是找出两个候选众数进行投票,需要两遍遍历,第一遍历找出两个候选众数,第二遍遍历重新投票验证这两个候选众数是否为众数即可. 实现代码 C++: class Solution

[LeetCode]27.Remove Element

[题目] Given an array and a value, remove all instances of that value in place and return the new length. The order of elements can be changed. It doesn't matter what you leave beyond the new length. [题意] 把数组中与给定值相同的元素删除,在原数组上修改,返回值是最终元素个数. [分析] 无 [代码]

[LeetCode] Find Peak Element

A peak element is an element that is greater than its neighbors. Given an input array where num[i] ≠ num[i+1], find a peak element and return its index. The array may contain multiple peaks, in that case return the index to any one of the peaks is fi

LeetCode 27 Remove Element(移除元素)

翻译 给定一个数组和一个值,删除该值的所有实例,并返回新的长度. 元素的顺序可以被改变,也不关心最终的数组长度. 原文 Given an array and a value, remove all instances of that value in place and return the new length. The order of elements can be changed. It doesn't matter what you leave beyond the new lengt

LeetCode All in One 题目讲解汇总(持续更新中...)

终于将LeetCode的免费题刷完了,真是漫长的第一遍啊,估计很多题都忘的差不多了,这次开个题目汇总贴,并附上每道题目的解题连接,方便之后查阅吧~ 如果各位看官们,大神们发现了任何错误,或是代码无法通过OJ,或是有更好的解法,或是有任何疑问,意见和建议的话,请一定要在对应的帖子下面评论区留言告知博主啊,多谢多谢,祝大家刷得愉快,刷得精彩,刷出美好未来- 博主制作了一款iOS的应用"Leetcode Meet Me",里面有Leetcode上所有的题目,并且贴上了博主的解法,随时随地都能