【題目描述】
Given an array of integers and a number k, the majority number is the number that occurs more than 1/k of the size of the array.Find it.
Notice:There is only one majority number in the array.
給定一個整型數(shù)組泉坐,找到主元素为鳄,它在數(shù)組中的出現(xiàn)次數(shù)嚴(yán)格大于數(shù)組元素個數(shù)的1/k。
注意:數(shù)組中只有唯一的主元素
【題目鏈接】
http://www.lintcode.com/en/problem/majority-number-iii/
【題目解析】
依然抵消法腕让,但是為了更快的獲取孤钦,消除,增加次數(shù)纯丸,這里應(yīng)該用hashtable(dictionary in python)
然后在剩下的數(shù)中找到出現(xiàn)次數(shù)最多的數(shù)偏形,就是majority number(因為前提是只有一個majority number)
如果不確定,可以再loop一次找出這個數(shù)的出現(xiàn)次數(shù)
【參考答案】