forked from tcandzq/LeetCode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMajorityElement.py
More file actions
38 lines (31 loc) · 945 Bytes
/
MajorityElement.py
File metadata and controls
38 lines (31 loc) · 945 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time : 2019/9/21 21:54
# @Author : tc
# @File : MajorityElement.py
"""
给定一个大小为 n 的数组,找到其中的众数。众数是指在数组中出现次数大于 ⌊ n/2 ⌋ 的元素。
你可以假设数组是非空的,并且给定的数组总是存在众数。
示例 1:
输入: [3,2,3]
输出: 3
示例 2:
输入: [2,2,1,1,1,2,2]
输出: 2
这题其实应该放在HashTable里
max(dict,dict.get):https://blog.csdn.net/weixin_41788255/article/details/79634142
"""
# Hash表解法
def majorityElement(nums):
if not nums:
return 0
appear_dict = {}
for num in nums:
if appear_dict.get(num):
appear_dict[num] += 1
else:
appear_dict[num] = 1
return max(appear_dict,key=appear_dict.get) # 这个max(dict,dict.get)是最骚的
if __name__ == '__main__':
nums = [3, 2, 3]
print(majorityElement(nums))