-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path229.py
More file actions
36 lines (32 loc) · 856 Bytes
/
229.py
File metadata and controls
36 lines (32 loc) · 856 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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time : 2020/4/20 15:01
# @Author : Fhh
# @File : 229.py
# Good good study,day day up!
"""
给定一个大小为 n 的数组,找出其中所有出现超过 ⌊ n/3 ⌋ 次的元素。
说明: 要求算法的时间复杂度为 O(n),空间复杂度为 O(1)。
示例 1:
输入: [3,2,3]
输出: [3]
示例 2:
输入: [1,1,1,3,3,2,2,2]
输出: [1,2]
"""
from typing import List
class Solution:
def majorityElement(self, nums: List[int]) -> List[int]:
dict = {}
res = []
for i in nums:
if i in dict:
dict[i] += 1
else:
dict[i] = 1
for i in dict:
if dict[i] > int(len(nums) / 3):
res.append(i)
return res
s = Solution()
print(s.majorityElement([1, 1, 1, 2, 2, 2, 3, 3]))