[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];
    }
};

目录
相关文章
Leetcode 230. Kth Smallest Element in a BST
先来看下二叉搜索树的性质,对于任意一个非叶子节点,它的左子树中所有的值必定小于这个节点的val,右子树中所有的值必定大于或等于当前节点的val。 这条性质就保证了如果我们对二叉搜索树做中序遍历,中序遍历的结果肯定是有序的。对于此题而言,我们只需要拿到中序遍历结果,然后返回第k个即可,时间复杂度是O(n)。
90 1
|
Python
LeetCode 378. Kth S Element in a Sorted Matrix
给定一个 n x n 矩阵,其中每行和每列元素均按升序排序,找到矩阵中第k小的元素。 请注意,它是排序后的第k小元素,而不是第k个元素。
147 0
LeetCode 378. Kth S Element in a Sorted Matrix
|
算法
LeetCode 229. Majority Element II
给定一个大小为 n 的数组,找出其中所有出现超过 ⌊ n/3 ⌋ 次的元素。 说明: 要求算法的时间复杂度为 O(n),空间复杂度为 O(1)。
178 0
LeetCode 229. Majority Element II
|
索引
LeetCode 162. Find Peak Element
给定一个输入数组 nums,其中 nums[i] ≠ nums[i+1],找到峰值元素并返回其索引。 数组可能包含多个峰值,在这种情况下,返回任何一个峰值所在位置即可。
135 0
LeetCode 162. Find Peak Element
|
算法 Python
LeetCode 169. 多数元素 Majority Element
LeetCode 169. 多数元素 Majority Element
LeetCode 215. Kth Largest Element in an Array
在未排序的数组中找到第 k 个最大的元素。请注意,你需要找的是数组排序后的第 k 个最大的元素,而不是第 k 个不同的元素。
176 0
|
Java C++
LeetCode之Remove Element
LeetCode之Remove Element
146 0
LeetCode之Next Greater Element I
LeetCode之Next Greater Element I
115 0
|
容器
LeetCode 169 Majority Element(主要元素)(vector、map)
版权声明:转载请联系本人,感谢配合!本站地址:http://blog.csdn.net/nomasp https://blog.csdn.net/NoMasp/article/details/50504698 翻译 给定一个长度为n的数组,找出主要的元素。
841 0
|
10月前
|
Unix Shell Linux
LeetCode刷题 Shell编程四则 | 194. 转置文件 192. 统计词频 193. 有效电话号码 195. 第十行
本文提供了几个Linux shell脚本编程问题的解决方案,包括转置文件内容、统计词频、验证有效电话号码和提取文件的第十行,每个问题都给出了至少一种实现方法。
142 6
LeetCode刷题 Shell编程四则 | 194. 转置文件 192. 统计词频 193. 有效电话号码 195. 第十行

热门文章

最新文章