-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path169.多数元素.py
62 lines (54 loc) · 1.27 KB
/
169.多数元素.py
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#
# @lc app=leetcode.cn id=169 lang=python3
#
# [169] 多数元素
#
# https://leetcode-cn.com/problems/majority-element/description/
#
# algorithms
# Easy (60.71%)
# Likes: 389
# Dislikes: 0
# Total Accepted: 93.4K
# Total Submissions: 153.6K
# Testcase Example: '[3,2,3]'
#
# 给定一个大小为 n 的数组,找到其中的多数元素。多数元素是指在数组中出现次数大于 ⌊ n/2 ⌋ 的元素。
#
# 你可以假设数组是非空的,并且给定的数组总是存在多数元素。
#
# 示例 1:
#
# 输入: [3,2,3]
# 输出: 3
#
# 示例 2:
#
# 输入: [2,2,1,1,1,2,2]
# 输出: 2
#
#
#
from typing import List
# @lc code=start
class Solution:
def majorityElement_1(self, nums: List[int]) -> int:
"""
1. 内置 Counter,哈希表
"""
from collections import Counter
counts = Counter(nums)
return max(counts.keys(), key=counts.get)
def majorityElement(self, nums: List[int]) -> int:
"""
2. 哈希表
"""
count = {}
for num in nums:
count[num] = count.get(num, 0) + 1
if count[num] > len(nums) // 2:
return num
return None
# 3. TODO: 太慢
# @lc code=end
Solution().majorityElement([3, 2, 3])