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.
public class Solution {
public int majorityElement(int[] nums) {
Arrays.sort(nums);
return nums[nums.length/2];
}
}
总结:这题给我做笑了,主要是给出的假设,肯定不为空,肯定有出现众数出现次数大于一半的,那这题很显然可以,把数组进行排序,中位数,一定就是众数。