File tree 1 file changed +27
-10
lines changed
1 file changed +27
-10
lines changed Original file line number Diff line number Diff line change @@ -37,9 +37,11 @@ Output: 2
37
37
38
38
## 代码
39
39
40
- ``` js
40
+ * 语言支持:JS,Python
41
41
42
+ Javascript Code:
42
43
44
+ ``` js
43
45
/*
44
46
* @lc app=leetcode id=169 lang=javascript
45
47
*
@@ -55,23 +57,23 @@ Output: 2
55
57
*
56
58
* Given an array of size n, find the majority element. The majority element is
57
59
* the element that appears more than ⌊ n/2 ⌋ times.
58
- *
60
+ *
59
61
* You may assume that the array is non-empty and the majority element always
60
62
* exist in the array.
61
- *
63
+ *
62
64
* Example 1:
63
- *
64
- *
65
+ *
66
+ *
65
67
* Input: [3,2,3]
66
68
* Output: 3
67
- *
69
+ *
68
70
* Example 2:
69
- *
70
- *
71
+ *
72
+ *
71
73
* Input: [2,2,1,1,1,2,2]
72
74
* Output: 2
73
- *
74
- *
75
+ *
76
+ *
75
77
*/
76
78
/**
77
79
* @param {number[]} nums
@@ -94,3 +96,18 @@ var majorityElement = function(nums) {
94
96
};
95
97
```
96
98
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
+ ```
You can’t perform that action at this time.
0 commit comments