Majority Element
Question
Given an array of size n, find the majority element. The majority element is the element that appears more than ⌊ n/2 ⌋
times.
You may assume that the array is non-empty and the majority element always exist in the array.
Answer
solution:
Knowledge:
可能这个算法并不是最佳的复杂度,但是终究能解决问题。此外,还在本地进行调试,从而帮助了这个程序。
字典的遍历方法:
结果为
0.00184039735833 0.00326492977712 0.00214993552657
,可见第一种方法速度最快。第二种方法的记忆方式为key,value在items中。
Last updated
Was this helpful?