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.
题意:找到给定的数组的众数。
思路:排序,然后取数组中间的数字。
class Solution {
public:
int majorityElement(vector<int>& nums) {
if (nums.empty())
return 0;
sort(nums.begin(), nums.end());
return nums[nums.size() / 2];
}
};