169. Majority Element
題目網址:https://leetcode.cn/problems/majority-element/
題意:給一大小為
n
的 arraynums
, 找出當中個數大於 $⌊ \dfrac{n}{2} ⌋$ 的數字(保證存在一個數滿足此條件)。進階:試著用 $O(n)$ time, $O(1)$ space 的演算法解決此問題
Solution 1:
想法:利用 hash table 紀錄
{num: count}
class Solution { |
- time:$O(n)$ ➔ 遍歷
nums
- space:$O(n)$ ➔
umap
Solution 2:
想法:利用 Boyer – Moore 投票法
- 當
res = 0
時, 將res
設為nums[i]
- 否則, 檢查
nums[i]
是否為res
。若是的話res + 1
, 否則res - 1
考慮特殊 case:
nums = [2,2,1,1,3]
, 得出res
= 3
nums[i] 2 2 1 1 3 cnt 1 2 1 0 1 res 2 2 2 2 3
nums = [2,2,3,4,5]
, 得出res
= 5
nums[i] 2 2 3 4 5 cnt 1 2 1 0 1 res 2 2 2 2 5 ➔ 以上情況皆不會在本題出現, 因為 2 的個數並沒有 > $⌊ \dfrac{5}{2} ⌋$
➔ 而本題的先決條件就是必有一數的個數 > $⌊ \dfrac{n}{2} ⌋$, 因此 Boyer – Moore 投票法在本題必成立
class Solution { |
- time:$O(n)$ ➔ 遍歷
nums
- space:$O(1)$ ➔ 只需常數空間
本部落格所有文章除特別聲明外,均採用 CC BY-NC-SA 4.0 許可協議。轉載請註明來自 Zako's Blog!
評論