-
Notifications
You must be signed in to change notification settings - Fork 25
Expand file tree
/
Copy path169_MajorityElement.py
More file actions
37 lines (27 loc) · 870 Bytes
/
Copy path169_MajorityElement.py
File metadata and controls
37 lines (27 loc) · 870 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
# coding: utf8
"""
题目链接: https://leetcode.com/problems/majority-element/description.
题目描述:
Given an array of size n, find the majority element. The majority element is the element that appears more than
⌊ n/2 ⌋ times.
You may assume that the array is non-empty and the majority element always exist in the array.
Credits:
Special thanks to @ts for adding this problem and creating all test cases.
"""
class Solution(object):
def majorityElement(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
ans = nums[0]
cnt = 1
for i in range(1, len(nums)):
if cnt == 0:
cnt += 1
ans = nums[i]
elif nums[i] == ans:
cnt += 1
else:
cnt -= 1
return ans