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.
因为保证有解,所以最后check的那一步在这题不需要,不过在L46就要。当然这题可以用hashmap数频率要O(n)space,或者可以先排序nlogn再过一遍来数数。又或者不排序,过两遍n方。