LeetCode 169 -Majority Element ( JAVA )

来源:互联网 发布:淘宝客的软件 编辑:程序博客网 时间:2024/04/29 10:24

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];    }}





总结:这题给我做笑了,主要是给出的假设,肯定不为空,肯定有出现众数出现次数大于一半的,那这题很显然可以,把数组进行排序,中位数,一定就是众数。

0 0
原创粉丝点击