[問題] leetcode 1287

看板Python作者時間4年前 (2020/04/23 22:55), 編輯推噓3(303)
留言6則, 4人參與, 4年前最新討論串1/1
題目: 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

04/23 23:29, 4年前 , 1F
dic[i] * 1.0 / len(arr) 就可以了
04/23 23:29, 1F

04/23 23:32, 4年前 , 2F
Python 2.X 的 / 假如沒有轉浮點數,預設是整數除法
04/23 23:32, 2F

04/23 23:42, 4年前 , 3F
同樓上,Python哪一版要搞清楚
04/23 23:42, 3F

04/24 00:27, 4年前 , 4F
既然都用Counter了怎麼不取most_commons就好,題目說
04/24 00:27, 4F

04/24 00:27, 4年前 , 5F
只會有一個解
04/24 00:27, 5F

04/29 21:46, 4年前 , 6F
同樓上 用most_commons解
04/29 21:46, 6F
文章代碼(AID): #1UeQnNCK (Python)