Skip to content

Commit

Permalink
feat: update solutions to lc/lcof/lcci problem: Majority Element
Browse files Browse the repository at this point in the history
  • Loading branch information
yanglbme committed Jun 12, 2021
1 parent b56d67f commit 4d7d4fa
Show file tree
Hide file tree
Showing 22 changed files with 573 additions and 100 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@

### 数组

- [多数元素](./solution/0100-0199/0169.Majority%20Element/README.md)
- [删除排序数组中的重复项](./solution/0000-0099/0026.Remove%20Duplicates%20from%20Sorted%20Array/README.md)
- [删除排序数组中的重复项 II](./solution/0000-0099/0080.Remove%20Duplicates%20from%20Sorted%20Array%20II/README.md)
- [移除元素](./solution/0000-0099/0027.Remove%20Element/README.md)
Expand Down
1 change: 1 addition & 0 deletions README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ Complete solutions to [LeetCode](https://leetcode-cn.com/problemset/all/), [LCOF

### Arrays

- [Majority Element](./solution/0100-0199/0169.Majority%20Element/README_EN.md)
- [Remove Duplicates from Sorted Array](./solution/0000-0099/0026.Remove%20Duplicates%20from%20Sorted%20Array/README_EN.md)
- [Remove Duplicates from Sorted Array II](./solution/0000-0099/0080.Remove%20Duplicates%20from%20Sorted%20Array%20II/README_EN.md)
- [Remove Element](./solution/0000-0099/0027.Remove%20Element/README_EN.md)
Expand Down
88 changes: 75 additions & 13 deletions lcci/17.10.Find Majority Element/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@

<!-- 这里可写通用的实现逻辑 -->

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

<!-- tabs:start -->

Expand All @@ -44,15 +44,37 @@
<!-- 这里可写当前语言的特殊实现逻辑 -->

```python

class Solution:
def majorityElement(self, nums: List[int]) -> int:
cnt = major = 0
for num in nums:
if cnt == 0:
major = num
cnt = 1
else:
cnt += (1 if major == num else -1)
return major
```

### **Java**

<!-- 这里可写当前语言的特殊实现逻辑 -->

```java

class Solution {
public int majorityElement(int[] nums) {
int cnt = 0, major = 0;
for (int num : nums) {
if (cnt == 0) {
major = num;
cnt = 1;
} else {
cnt += (major == num ? 1 : -1);
}
}
return major;
}
}
```

### **JavaScript**
Expand All @@ -63,23 +85,63 @@
* @return {number}
*/
var majorityElement = function(nums) {
let candidate = 0, count = 0;
for (let num of nums) {
if (count == 0) candidate = num;
if (candidate == num) {
count++;
let cnt = 0;
let major = 0;
for (const num of nums) {
if (cnt == 0) {
major = num;
cnt = 1;
} else {
count--;
cnt += (major == num ? 1 : -1);
}
}
let n = 0;
for (let num of nums) {
if (candidate == num) n++;
return major;
};
```

### **C++**

```cpp
class Solution {
public:
int majorityElement(vector<int>& nums) {
int cnt = 0, major = 0;
for (int num : nums) {
if (cnt == 0) {
major = num;
cnt = 1;
} else {
cnt += (major == num ? 1 : -1);
}
}
return major;
}
return n > (nums.length / 2) ? candidate : -1;
};
```
### **C#**
```cs
public class Solution {
public int MajorityElement(int[] nums) {
int cnt = 0, major = 0;
foreach (int num in nums)
{
if (cnt == 0)
{
major = num;
cnt = 1;
}
else
{
cnt += (major == num ? 1 : -1);
}
}
return major;
}
}
```

### **...**

```
Expand Down
86 changes: 74 additions & 12 deletions lcci/17.10.Find Majority Element/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,35 @@ Boyer–Moore majority vote algorithm
### **Python3**

```python

class Solution:
def majorityElement(self, nums: List[int]) -> int:
cnt = major = 0
for num in nums:
if cnt == 0:
major = num
cnt = 1
else:
cnt += (1 if major == num else -1)
return major
```

### **Java**

```java

class Solution {
public int majorityElement(int[] nums) {
int cnt = 0, major = 0;
for (int num : nums) {
if (cnt == 0) {
major = num;
cnt = 1;
} else {
cnt += (major == num ? 1 : -1);
}
}
return major;
}
}
```

### **JavaScript**
Expand All @@ -62,23 +84,63 @@ Boyer–Moore majority vote algorithm
* @return {number}
*/
var majorityElement = function(nums) {
let candidate = 0, count = 0;
for (let num of nums) {
if (count == 0) candidate = num;
if (candidate == num) {
count++;
let cnt = 0;
let major = 0;
for (const num of nums) {
if (cnt == 0) {
major = num;
cnt = 1;
} else {
count--;
cnt += (major == num ? 1 : -1);
}
}
let n = 0;
for (let num of nums) {
if (candidate == num) n++;
return major;
};
```

### **C++**

```cpp
class Solution {
public:
int majorityElement(vector<int>& nums) {
int cnt = 0, major = 0;
for (int num : nums) {
if (cnt == 0) {
major = num;
cnt = 1;
} else {
cnt += (major == num ? 1 : -1);
}
}
return major;
}
return n > (nums.length / 2) ? candidate : -1;
};
```
### **C#**
```cs
public class Solution {
public int MajorityElement(int[] nums) {
int cnt = 0, major = 0;
foreach (int num in nums)
{
if (cnt == 0)
{
major = num;
cnt = 1;
}
else
{
cnt += (major == num ? 1 : -1);
}
}
return major;
}
}
```

### **...**

```
Expand Down
15 changes: 15 additions & 0 deletions lcci/17.10.Find Majority Element/Solution.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
class Solution {
public:
int majorityElement(vector<int>& nums) {
int cnt = 0, major = 0;
for (int num : nums) {
if (cnt == 0) {
major = num;
cnt = 1;
} else {
cnt += (major == num ? 1 : -1);
}
}
return major;
}
};
18 changes: 18 additions & 0 deletions lcci/17.10.Find Majority Element/Solution.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
public class Solution {
public int MajorityElement(int[] nums) {
int cnt = 0, major = 0;
foreach (int num in nums)
{
if (cnt == 0)
{
major = num;
cnt = 1;
}
else
{
cnt += (major == num ? 1 : -1);
}
}
return major;
}
}
16 changes: 16 additions & 0 deletions lcci/17.10.Find Majority Element/Solution.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
func majorityElement(nums []int) int {
var cnt, major int
for _, num := range nums {
if cnt == 0 {
major = num
cnt = 1
} else {
if major == num {
cnt++
} else {
cnt--
}
}
}
return major
}
14 changes: 14 additions & 0 deletions lcci/17.10.Find Majority Element/Solution.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
class Solution {
public int majorityElement(int[] nums) {
int cnt = 0, major = 0;
for (int num : nums) {
if (cnt == 0) {
major = num;
cnt = 1;
} else {
cnt += (major == num ? 1 : -1);
}
}
return major;
}
}
19 changes: 8 additions & 11 deletions lcci/17.10.Find Majority Element/Solution.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,15 @@
* @return {number}
*/
var majorityElement = function(nums) {
let candidate = 0, count = 0;
for (let num of nums) {
if (count == 0) candidate = num;
if (candidate == num) {
count++;
let cnt = 0;
let major = 0;
for (const num of nums) {
if (cnt == 0) {
major = num;
cnt = 1;
} else {
count--;
cnt += (major == num ? 1 : -1);
}
}
let n = 0;
for (let num of nums) {
if (candidate == num) n++;
}
return n > (nums.length / 2) ? candidate : -1;
return major;
};
10 changes: 10 additions & 0 deletions lcci/17.10.Find Majority Element/Solution.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
class Solution:
def majorityElement(self, nums: List[int]) -> int:
cnt = major = 0
for num in nums:
if cnt == 0:
major = num
cnt = 1
else:
cnt += (1 if major == num else -1)
return major
Loading

0 comments on commit 4d7d4fa

Please sign in to comment.