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.

//出现次数超过数组长度一半的元素,排序取出中间位置的元素即可

class Solution {
public:
    int majorityElement(vector<int>& nums) {
        if(nums.size()==0)
            return -1;
        
        sort(nums.begin(),nums.end());
        int mid = nums.size()/2;
        
        return nums[mid];
    }
};

 

posted on 2017-03-06 11:48  123_123  阅读(76)  评论(0编辑  收藏  举报