-
-
Notifications
You must be signed in to change notification settings - Fork 291
Expand file tree
/
Copy path229.java
More file actions
74 lines (72 loc) · 2.31 KB
/
229.java
File metadata and controls
74 lines (72 loc) · 2.31 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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
__________________________________________________________________________________________________
sample 1 ms submission
class Solution {
public List<Integer> majorityElement(int[] nums) {
List<Integer> result = new ArrayList<Integer>();
if(nums.length == 0){
return result;
}
int candidateA = nums[0];
int candidateB = nums[0];
int countA = 0;
int countB = 0;
for(int num : nums){
if(num == candidateA){
countA += 1;
}else if(num == candidateB){
countB += 1;
}else{
if(countA == 0){
candidateA = num;
countA = 1;
}else if(countB == 0){
candidateB = num;
countB = 1;
}else{
countA -= 1;
countB -= 1;
}
}
}
countA = 0;
countB = 0;
for(int e : nums){
if(e == candidateA){
countA += 1;
}else if(e == candidateB){
countB += 1;
}
}
if(countA > nums.length / 3){
result.add(candidateA);
}
if(countB > nums.length / 3){
result.add(candidateB);
}
return result;
}
}
__________________________________________________________________________________________________
sample 36872 kb submission
class Solution {
public List<Integer> majorityElement(int[] nums) {
List<Integer> ret = new ArrayList<Integer>();
int occur = nums.length/3;
HashMap<Integer, Integer> occurences = new HashMap<Integer, Integer>();
for(int i = 0; i < nums.length; i++){
if(occurences.containsKey(nums[i])){
occurences.put(nums[i], occurences.get(nums[i])+1);
}else{
occurences.put(nums[i], 1);
}
}
for (Map.Entry mapElement : occurences.entrySet()) {
int compare = (int) mapElement.getValue();
if(compare > occur){
ret.add((int)mapElement.getKey());
}
}
return ret;
}
}
__________________________________________________________________________________________________