Q169. Majority Element
Last updated
Was this helpful?
Last updated
Was this helpful?
直达:
Given an array of sizen, find the majority element. The majority element is the element that appearsmore than⌊ n/2 ⌋
times.
You may assume that the array is non-empty and the majority element always exist in the array.
本题很容易想到基于排序或者哈希表的方法,但是排序的方法的时间复杂度是O(nlogn),而哈希表需要占用额外的存储空间,若要使用O(n)且不占用额外存储空间的方法,可以使用Boyer-Moore投票方法
Boyer-Moore投票方法是每次删除两个不同的数,直到最后剩下的一定是出现次数大于的数,但是数组的删除是非常困难的,因为每删除一个元素都要移动数组,所以可以采用计数器的方式。
设当前统计的数是flag,遇到等于flag的数count+1,否则count - 1,如果count < 0, 则表示该数不一定是要返回的数,则可以换一个数。