Skip to content

Commit 25c5b85

Browse files
ybian19azl397985856
authored andcommitted
feat: 169.majority-element add Python3 implementation (azl397985856#108)
1 parent 69b2fc7 commit 25c5b85

File tree

1 file changed

+27
-10
lines changed

1 file changed

+27
-10
lines changed

problems/169.majority-element.md

+27-10
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,11 @@ Output: 2
3737

3838
## 代码
3939

40-
```js
40+
* 语言支持:JS,Python
4141

42+
Javascript Code:
4243

44+
```js
4345
/*
4446
* @lc app=leetcode id=169 lang=javascript
4547
*
@@ -55,23 +57,23 @@ Output: 2
5557
*
5658
* Given an array of size n, find the majority element. The majority element is
5759
* the element that appears more than ⌊ n/2 ⌋ times.
58-
*
60+
*
5961
* You may assume that the array is non-empty and the majority element always
6062
* exist in the array.
61-
*
63+
*
6264
* Example 1:
63-
*
64-
*
65+
*
66+
*
6567
* Input: [3,2,3]
6668
* Output: 3
67-
*
69+
*
6870
* Example 2:
69-
*
70-
*
71+
*
72+
*
7173
* Input: [2,2,1,1,1,2,2]
7274
* Output: 2
73-
*
74-
*
75+
*
76+
*
7577
*/
7678
/**
7779
* @param {number[]} nums
@@ -94,3 +96,18 @@ var majorityElement = function(nums) {
9496
};
9597
```
9698

99+
Python Code:
100+
101+
```python
102+
class Solution:
103+
def majorityElement(self, nums: List[int]) -> int:
104+
count, majority = 1, nums[0]
105+
for num in nums[1:]:
106+
if count == 0:
107+
majority = num
108+
if num == majority:
109+
count += 1
110+
else:
111+
count -= 1
112+
return majority
113+
```

0 commit comments

Comments
 (0)