-
Notifications
You must be signed in to change notification settings - Fork 0
/
MajorityElementII.java
47 lines (39 loc) · 1.23 KB
/
MajorityElementII.java
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
38
39
40
41
42
43
44
45
46
47
import java.util.*;
public class MajorityElementII {
public List<Integer> majorityElement(int[] nums) {
Arrays.sort(nums);
if (nums.length < 3) {
if (nums.length == 2) {
if (nums[0] == nums[1]) {
return List.of(nums[0]);
}
}
List<Integer> intList = new ArrayList<>(nums.length);
for (int i : nums) {
intList.add(i);
}
return intList;
}
int buffer = 1;
List<Integer> result = new ArrayList<>();
for (int i = 1; i < nums.length; i++) {
if (nums[i] == nums[i - 1]) {
buffer++;
if (buffer > nums.length / 3) {
result.add(nums[i]);
while (i < nums.length - 1 && nums[i + 1] == nums[i]) {
i++;
}
}
} else {
buffer = 1;
}
}
return result;
}
public static void main(String[] args) {
MajorityElementII majorityElement = new MajorityElementII();
int[] nums = {1,1,1,2,2,2};
System.out.println(majorityElement.majorityElement(nums));
}
}