Leetcode腾讯精选_编号:169 --python
2019-02-28 20:45
393 查看
给定一个大小为 n 的数组,找到其中的众数。众数是指在数组中出现次数大于 ⌊ n/2 ⌋ 的元素。
你可以假设数组是非空的,并且给定的数组总是存在众数。
示例 1:
输入: [3,2,3]
输出: 3
示例 2:
输入: [2,2,1,1,1,2,2]
输出: 2
class Solution: def majorityElement(self, nums): """ :type nums: List[int] :rtype: int """ lookup = {} for num in nums: lookup[num] = lookup.get(num, 0) + 1 max_occur = max(lookup.values()) for key in lookup: if lookup[key] == max_occur: return key
相关文章推荐
- Leetcode腾讯精选_编号:206 --python
- Leetcode腾讯精选_编号:217 --python
- Leetcode腾讯精选_编号:231 --python
- Leetcode腾讯精选_编号:235 --python
- Leetcode腾讯精选_编号:236 --python
- Leetcode腾讯精选_编号:237 --python
- Leetcode腾讯精选_编号:292 --python
- Leetcode腾讯精选_编号:344 --python
- Leetcode腾讯精选_编号:557 --python
- Leetcode腾讯精选_编号:230 --java
- LeetCode 169. Majority Element(Python)
- 【LeetCode】Python实现-169. 求众数
- leetcode【169 Majority Element】【Python】
- leetcode 169.求众数(python)
- LeetCode 62.Unique Paths---Python实现
- Leetcode 35_搜索插入位置 --python
- LeetCode 63. Unique Paths II (Python)最优解--Apolo_Shane
- leetcode_102. Binary Tree Level Order Traversal 二叉树层次遍历,python实现
- [Leetcode]@python 75. Sort Colors
- leetcode 【 Partition List 】python 实现