【題目描述】
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.
給定一個(gè)整型數(shù)組,找到主元素,它在數(shù)組中的出現(xiàn)次數(shù)嚴(yán)格大于數(shù)組元素個(gè)數(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(因?yàn)榍疤崾侵挥幸粋€(gè)majority number)
如果不確定,可以再loop一次找出這個(gè)數(shù)的出現(xiàn)次數(shù)
【參考答案】