p169

LeetCode 169 Majority Element 题解

1.题目:

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.

题意:

输入一个数组,取出其中一个出现次数大于n/2的数

2.解题思路:

排序取中间

3.代码


[title] [] [url] [link text]
1
2
3
4
5
6
7
8
9
10
11
12
 

public class Solution {
public int majorityElement(int[] nums) {
int a=0;
Arrays.sort(nums);
a=nums[nums.length/2];

return a;
}
}


4.一些总结: