-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path169.py
37 lines (30 loc) · 886 Bytes
/
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
class Solution:
# @param {integer[]} nums
# @return {integer}
def majorityElement(self, nums):
nums_len = len(nums)
numdict = dict()
for n in nums:
numdict.setdefault(n,0)
numdict[n] += 1
max_time = nums_len/2
max_num = 0
for key in numdict.keys():
if numdict[key] > max_time:
max_time = numdict[key]
max_num = key
return max_num
# new solution
def majorityElement(self, nums):
maj = nums[0]
pointer = 1
for n in nums[1:]:
if pointer == 0:
maj = n
pointer = 1
else:
if n == maj:
pointer += 1
else:
pointer -= 1
return maj