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.

// Source : https://oj.leetcode.com/problems/majority-element/
// Author : Chao Zeng
// Date   : 2014-12-22
class Solution {
public:
    int majorityElement(vector<int> &num) {
        int n = num.size();
        sort(num.begin(),num.end());
        return num[n/2];
    }
};


你可能感兴趣的:(LeetCode,sort)