在一个长度为n的数组中,寻找出现次数大于n/2的数
169. Majority Element
Given an array of size n, find the majority element. The majority element is the element that appears more thann/2times.
You may assume that the array is non-empty and the majority element always exist in the array.
最直接的思路,用一个字典保存所有数出现的次数,当出现大于n/2的直接返回
1 |
class (object): |
看到别人的思路,把数组排序,中间的值一定是出现次数大于等于2的值
1 |
class (object): |
近期评论