Leetcode 169.多数元素 (每日一题 20210715)
生活随笔
收集整理的這篇文章主要介紹了
Leetcode 169.多数元素 (每日一题 20210715)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
給定一個大小為 n 的數組,找到其中的多數元素。多數元素是指在數組中出現次數 大于?? n/2 ??的元素。你可以假設數組是非空的,并且給定的數組總是存在多數元素。示例?1:輸入:[3,2,3]
輸出:3
示例?2:輸入:[2,2,1,1,1,2,2]
輸出:2鏈接:https://leetcode-cn.com/problems/majority-elementclass Solution:def majorityElement(self, nums: List[int]) -> int:current, count = 0, 0for item in nums:if count == 0:current = itemif current == item:count += 1else:count -= 1return current
總結
以上是生活随笔為你收集整理的Leetcode 169.多数元素 (每日一题 20210715)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Leetcode 136.只出现一次的数
- 下一篇: Leetcode 剑指offer 22.