-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQ229_MajorityElementII.java
More file actions
50 lines (46 loc) · 1.19 KB
/
Copy pathQ229_MajorityElementII.java
File metadata and controls
50 lines (46 loc) · 1.19 KB
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
48
49
50
package leetcode2.array;
import java.util.LinkedList;
import java.util.List;
public class Q229_MajorityElementII {
public static List<Integer> majorityElement(int[] nums) {
int num1 = 0;
int num2 = 0;
int c1 = 0;
int c2 = 0;
for(int x:nums){
if(num1==x){
c1++;
}else if(num2==x){
c2++;
}else if(c1==0){
num1 = x;
c1 = 1;
}else if(c2==0){
num2 = x;
c2 = 1;
}else{
c1--;
c2--;
}
}
c1 = 0;
c2 = 0;
for(int x:nums){
if(x==num1){
c1++;
}else if(x==num2){
c2++;
}
}
List<Integer> ans = new LinkedList<Integer>();
if(c1>nums.length/3) ans.add(num1);
if(c2>nums.length/3) ans.add(num2);
return ans;
}
public static void main(String[] args) {
List<Integer> ans = majorityElement(new int[]{1,1,1,2,2,2,2,2,2,3,3,3,3,3,3});
for(int i:ans){
System.out.println(i);
}
}
}