- public class Solution {
- /*
- * @param nums: a list of integers
- * @return: find a majority number
- */
- public int majorityNumber(List < Integer > nums) {
- // write your code here
- int max = 0;
- int len = nums.size();
- Collections.sort(nums);
- int temp = nums.get(len / 2);
- for (int i = 0; i < len; i++) {
- if (temp == nums.get(i)) {
- max++;
- }
- }
- if (max > len / 2) {
- return temp;
- } else {
- return 0;
- }
- }
- }
来源: http://www.bubuko.com/infodetail-2490045.html