看板 Python 關於我們 聯絡資訊
題目: Given an integer array sorted in non-decreasing order, there is exactly one integer in the array that occurs more than 25% of the time. Return that integer. Example 1: Input: arr = [1,2,2,6,6,6,6,7,10] Output: 6 Constraints: 1 <= arr.length <= 10^4 0 <= arr[i] <= 10^5 code: class Solution(object): def findSpecialInteger(self, arr): """ :type arr: List[int] :rtype: int """ dic={} dic=Counter(arr) for i in dic: if (dic[i]/len(arr))>0.25: return i 問題: 我用Visual Studio Code的編譯器跑出來沒問題,但leetcode會跑出None,不知道 哪裡出問題了 -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 49.216.60.243 (臺灣) ※ 文章網址: https://www.ptt.cc/bbs/Python/M.1587653719.A.314.html
cuteSquirrel: dic[i] * 1.0 / len(arr) 就可以了 04/23 23:29
cuteSquirrel: Python 2.X 的 / 假如沒有轉浮點數,預設是整數除法 04/23 23:32
mirror0227: 同樓上,Python哪一版要搞清楚 04/23 23:42
moodoa3583: 既然都用Counter了怎麼不取most_commons就好,題目說 04/24 00:27
moodoa3583: 只會有一個解 04/24 00:27
Jyery: 同樓上 用most_commons解 04/29 21:46