1. 程式人生 > >169. Majority Element 出現次數超過n/2的元素

169. Majority Element 出現次數超過n/2的元素

結構 find array ray appear 暴力 OS 不用 div

[抄題]:

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.

[暴力解法]:

時間分析:

空間分析:

[奇葩輸出條件]:

[奇葩corner case]:

[思維問題]:

以為n個元素的出現次數一定也要用n個變量來存。

[一句話思路]:

用一個count來存最多次數,節約空間

[輸入量]:空: 正常情況:特大:特小:程序裏處理到的特殊情況:異常情況(不合法不合理的輸入):

[畫圖]:

[一刷]:

[二刷]:

[三刷]:

[四刷]:

[五刷]:

[五分鐘肉眼debug的結果]:

[總結]:

[復雜度]:Time complexity: O() Space complexity: O()

[英文數據結構或算法,為什麽不用別的數據結構或算法]:

[關鍵模板化代碼]:

[其他解法]:

[Follow Up]:

[LC給出的題目變變變]:

[代碼風格] :

169. Majority Element 出現次數超過n/2的元素