Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit b9d8aab

Browse files
committed
feat: add solutions to lc/lcof/lcci problems
lcci No.17.10.Find Majority Element lc No.0229.Majority Element II
1 parent 9a82099 commit b9d8aab

File tree

21 files changed

+774
-253
lines changed

21 files changed

+774
-253
lines changed

‎lcci/17.10.Find Majority Element/README.md‎

Lines changed: 82 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,19 @@
3737

3838
摩尔投票法。时间复杂度 O(n),空间复杂度 O(1)。
3939

40+
一般而言,摩尔投票法需要对输入的列表进行**两次遍历**。在第一次遍历中,我们生成候选值 candidate,如果存在多数,那么该候选值就是多数值。在第二次遍历中,只需要简单地计算候选值的频率,以确认是否是多数值。
41+
42+
接下来我们详细看下**第一次遍历**:
43+
44+
我们需要两个变量:`cnt`, `candidate`,其中 `cnt` 初始化为 0,`candidate` 初始化可以是任何值,这里我们设置为 0。
45+
46+
对于列表中的每个元素 num,我们首先检查计数值 cnt,
47+
48+
-`cnt == 0`,我们将候选值 candidate 设置为当前元素值,即 `candidate = num`
49+
-`candidate == num`,将 cnt 加 1,否则减 1。
50+
51+
**第二次遍历**,则是扫描列表中 candidate 出现的次数,若大于 `n/2`,则该候选值就是多数值,否则返回 -1。
52+
4053
<!-- tabs:start -->
4154

4255
### **Python3**
@@ -46,14 +59,12 @@
4659
```python
4760
class Solution:
4861
def majorityElement(self, nums: List[int]) -> int:
49-
cnt = major = 0
62+
cnt = candidate = 0
5063
for num in nums:
5164
if cnt == 0:
52-
major = num
53-
cnt = 1
54-
else:
55-
cnt += (1 if major == num else -1)
56-
return major
65+
candidate = num
66+
cnt += (1 if candidate == num else -1)
67+
return candidate if nums.count(candidate) > len(nums) / 2 else -1
5768
```
5869

5970
### **Java**
@@ -63,16 +74,20 @@ class Solution:
6374
```java
6475
class Solution {
6576
public int majorityElement(int[] nums) {
66-
int cnt = 0, major = 0;
77+
int cnt = 0, candidate = 0;
6778
for (int num : nums) {
6879
if (cnt == 0) {
69-
major = num;
70-
cnt = 1;
71-
} else {
72-
cnt += (major == num ? 1 : -1);
80+
candidate = num;
7381
}
82+
cnt += (num == candidate ? 1 : -1);
7483
}
75-
return major;
84+
cnt = 0;
85+
for (int num : nums) {
86+
if (num == candidate) {
87+
++cnt;
88+
}
89+
}
90+
return cnt > nums.length / 2 ? candidate : -1;
7691
}
7792
}
7893
```
@@ -86,16 +101,20 @@ class Solution {
86101
*/
87102
var majorityElement = function(nums) {
88103
let cnt = 0;
89-
let major = 0;
104+
let candidate = 0;
90105
for (const num of nums) {
91106
if (cnt == 0) {
92-
major = num;
93-
cnt = 1;
94-
} else {
95-
cnt += (major == num ? 1 : -1);
107+
candidate = num;
108+
}
109+
cnt += (candidate == num ? 1 : -1);
110+
}
111+
cnt = 0;
112+
for (const num of nums) {
113+
if (candidate == num) {
114+
++cnt;
96115
}
97116
}
98-
return major;
117+
return cnt >nums.length/2? candidate :-1;
99118
};
100119
```
101120

@@ -105,39 +124,69 @@ var majorityElement = function(nums) {
105124
class Solution {
106125
public:
107126
int majorityElement(vector<int>& nums) {
108-
int cnt = 0, major = 0;
109-
for (int num : nums) {
110-
if (cnt == 0) {
111-
major = num;
112-
cnt = 1;
113-
} else {
114-
cnt += (major == num ? 1 : -1);
115-
}
127+
int cnt = 0, candidate = 0;
128+
for (int num : nums)
129+
{
130+
if (cnt == 0) candidate = num;
131+
cnt += (candidate == num ? 1 : -1);
116132
}
117-
return major;
133+
cnt = count(nums.begin(), nums.end(), candidate);
134+
return cnt > nums.size() / 2 ? candidate : -1;
118135
}
119136
};
120137
```
121138
139+
### **Go**
140+
141+
```go
142+
func majorityElement(nums []int) int {
143+
var cnt, candidate int
144+
for _, num := range nums {
145+
if cnt == 0 {
146+
candidate = num
147+
}
148+
if candidate == num {
149+
cnt++
150+
} else {
151+
cnt--
152+
}
153+
}
154+
cnt = 0
155+
for _, num := range nums {
156+
if candidate == num {
157+
cnt++
158+
}
159+
}
160+
if cnt > len(nums)/2 {
161+
return candidate
162+
}
163+
return -1
164+
}
165+
```
166+
122167
### **C#**
123168

124169
```cs
125170
public class Solution {
126171
public int MajorityElement(int[] nums) {
127-
int cnt = 0, major = 0;
172+
int cnt = 0, candidate = 0;
128173
foreach (int num in nums)
129174
{
130175
if (cnt == 0)
131176
{
132-
major = num;
133-
cnt = 1;
177+
candidate = num;
134178
}
135-
else
179+
cnt += (candidate == num ? 1 : -1);
180+
}
181+
cnt = 0;
182+
foreach (int num in nums)
183+
{
184+
if (candidate == num)
136185
{
137-
cnt += (major == num ? 1 : -1);
186+
++cnt;
138187
}
139188
}
140-
return major;
189+
return cnt>nums.Length/2?candidate:-1;
141190
}
142191
}
143192
```

‎lcci/17.10.Find Majority Element/README_EN.md‎

Lines changed: 69 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -47,31 +47,33 @@ Boyer–Moore majority vote algorithm
4747
```python
4848
class Solution:
4949
def majorityElement(self, nums: List[int]) -> int:
50-
cnt = major = 0
50+
cnt = candidate = 0
5151
for num in nums:
5252
if cnt == 0:
53-
major = num
54-
cnt = 1
55-
else:
56-
cnt += (1 if major == num else -1)
57-
return major
53+
candidate = num
54+
cnt += (1 if candidate == num else -1)
55+
return candidate if nums.count(candidate) > len(nums) / 2 else -1
5856
```
5957

6058
### **Java**
6159

6260
```java
6361
class Solution {
6462
public int majorityElement(int[] nums) {
65-
int cnt = 0, major = 0;
63+
int cnt = 0, candidate = 0;
6664
for (int num : nums) {
6765
if (cnt == 0) {
68-
major = num;
69-
cnt = 1;
70-
} else {
71-
cnt += (major == num ? 1 : -1);
66+
candidate = num;
7267
}
68+
cnt += (num == candidate ? 1 : -1);
7369
}
74-
return major;
70+
cnt = 0;
71+
for (int num : nums) {
72+
if (num == candidate) {
73+
++cnt;
74+
}
75+
}
76+
return cnt > nums.length / 2 ? candidate : -1;
7577
}
7678
}
7779
```
@@ -85,16 +87,20 @@ class Solution {
8587
*/
8688
var majorityElement = function(nums) {
8789
let cnt = 0;
88-
let major = 0;
90+
let candidate = 0;
8991
for (const num of nums) {
9092
if (cnt == 0) {
91-
major = num;
92-
cnt = 1;
93-
} else {
94-
cnt += (major == num ? 1 : -1);
93+
candidate = num;
94+
}
95+
cnt += (candidate == num ? 1 : -1);
96+
}
97+
cnt = 0;
98+
for (const num of nums) {
99+
if (candidate == num) {
100+
++cnt;
95101
}
96102
}
97-
return major;
103+
return cnt >nums.length/2? candidate :-1;
98104
};
99105
```
100106

@@ -104,39 +110,69 @@ var majorityElement = function(nums) {
104110
class Solution {
105111
public:
106112
int majorityElement(vector<int>& nums) {
107-
int cnt = 0, major = 0;
108-
for (int num : nums) {
109-
if (cnt == 0) {
110-
major = num;
111-
cnt = 1;
112-
} else {
113-
cnt += (major == num ? 1 : -1);
114-
}
113+
int cnt = 0, candidate = 0;
114+
for (int num : nums)
115+
{
116+
if (cnt == 0) candidate = num;
117+
cnt += (candidate == num ? 1 : -1);
115118
}
116-
return major;
119+
cnt = count(nums.begin(), nums.end(), candidate);
120+
return cnt > nums.size() / 2 ? candidate : -1;
117121
}
118122
};
119123
```
120124
125+
### **Go**
126+
127+
```go
128+
func majorityElement(nums []int) int {
129+
var cnt, candidate int
130+
for _, num := range nums {
131+
if cnt == 0 {
132+
candidate = num
133+
}
134+
if candidate == num {
135+
cnt++
136+
} else {
137+
cnt--
138+
}
139+
}
140+
cnt = 0
141+
for _, num := range nums {
142+
if candidate == num {
143+
cnt++
144+
}
145+
}
146+
if cnt > len(nums)/2 {
147+
return candidate
148+
}
149+
return -1
150+
}
151+
```
152+
121153
### **C#**
122154

123155
```cs
124156
public class Solution {
125157
public int MajorityElement(int[] nums) {
126-
int cnt = 0, major = 0;
158+
int cnt = 0, candidate = 0;
127159
foreach (int num in nums)
128160
{
129161
if (cnt == 0)
130162
{
131-
major = num;
132-
cnt = 1;
163+
candidate = num;
133164
}
134-
else
165+
cnt += (candidate == num ? 1 : -1);
166+
}
167+
cnt = 0;
168+
foreach (int num in nums)
169+
{
170+
if (candidate == num)
135171
{
136-
cnt += (major == num ? 1 : -1);
172+
++cnt;
137173
}
138174
}
139-
return major;
175+
return cnt>nums.Length/2?candidate:-1;
140176
}
141177
}
142178
```
Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,13 @@
11
class Solution {
22
public:
33
int majorityElement(vector<int>& nums) {
4-
int cnt = 0, major = 0;
5-
for (int num : nums) {
6-
if (cnt == 0) {
7-
major = num;
8-
cnt = 1;
9-
} else {
10-
cnt += (major == num ? 1 : -1);
11-
}
4+
int cnt = 0, candidate = 0;
5+
for (int num : nums)
6+
{
7+
if (cnt == 0) candidate = num;
8+
cnt += (candidate == num ? 1 : -1);
129
}
13-
return major;
10+
cnt = count(nums.begin(), nums.end(), candidate);
11+
return cnt > nums.size() / 2 ? candidate : -1;
1412
}
1513
};
Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,22 @@
11
public class Solution {
22
public int MajorityElement(int[] nums) {
3-
int cnt = 0, major = 0;
3+
int cnt = 0, candidate = 0;
44
foreach (int num in nums)
55
{
66
if (cnt == 0)
77
{
8-
major = num;
9-
cnt = 1;
8+
candidate = num;
109
}
11-
else
10+
cnt += (candidate == num ? 1 : -1);
11+
}
12+
cnt = 0;
13+
foreach (int num in nums)
14+
{
15+
if (candidate == num)
1216
{
13-
cnt+=(major==num?1:-1);
17+
++cnt;
1418
}
1519
}
16-
return major;
20+
return cnt>nums.Length/2?candidate:-1;
1721
}
1822
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /