matteo's coding field.

LeetCode.169 Majority Element

字数统计: 89阅读时长: 1 min
2019/05/27 Share

https://leetcode.com/problems/majority-element/

题意
  • 找出一个无序数列中占多数的数
    解法
  1. 排序后(n/2位置即结果)位置即结果
  2. https://en.wikipedia.org/wiki/Boyer%E2%80%93Moore_majority_vote_algorithm
class Solution {
    public int majorityElement(int[] nums) {
        //Boyer Moore Voting Algorithm
        int count = 0, candidateIdx = -1;
        for (int i = 0; i < nums.length; i++) {
            if (count == 0) {
                candidateIdx = i;
            }
            if (nums[i] == nums[candidateIdx]) {
                count++;
            } else {
                count--;
            }
        }
        return nums[candidateIdx];

    }
}
CATALOG
  1. 1. 题意
  2. 2. 解法