-
Notifications
You must be signed in to change notification settings - Fork 319
/
Copy path229_MajorityElementII.py
38 lines (36 loc) · 992 Bytes
/
229_MajorityElementII.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
#! /usr/bin/env python
# -*- coding: utf-8 -*-
# @Author: [email protected]
# According to:
# https://leetcode.com/discuss/43248/boyer-moore-majority-vote-algorithm-and-my-elaboration
class Solution(object):
def majorityElement(self, nums):
if not nums:
return []
candidate_1, candidate_2 = 0, 1
count_1, count_2 = 0, 0
for num in nums:
if num == candidate_1:
count_1 += 1
elif num == candidate_2:
count_2 += 1
elif not count_1:
candidate_1, count_1 = num, 1
elif not count_2:
candidate_2, count_2 = num, 1
else:
count_1 -= 1
count_2 -= 1
result = []
for num in [candidate_1, candidate_2]:
if nums.count(num) > len(nums) / 3:
result.append(num)
return result
"""
[]
[0,0,0]
[1,2,2,3,3,1,1,1]
[2,2,2,3,3,4,3,2]
[1,1,2]
[3,0,3,4]
"""