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.

 

超简单的一道题,排序后中间的数据即是结果

public class Solution {
    public int majorityElement(int[] nums) {
        if (nums==null||nums.length==0) return 0;
        Arrays.sort(nums);
        return nums[nums.length/2];
    }
}

 

posted on 2017-07-11 11:23  sure0328  阅读(62)  评论(0)    收藏  举报

导航