-
Notifications
You must be signed in to change notification settings - Fork 38
Expand file tree
/
Copy pathSolution.java
More file actions
65 lines (60 loc) · 1.72 KB
/
Copy pathSolution.java
File metadata and controls
65 lines (60 loc) · 1.72 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
/**
* Time : O(); Space : O()
* @tag : Array
* @by : Steven Cooks
* @date: Jul 6, 2015
*******************************************************************************
* Description:
*
* Given an integer array of size n, find all elements that appear more
* than ⌊ n/3 ⌋ times. The algorithm should run in linear time and in O(1) space.
*
*******************************************************************************
* {@link https://leetcode.com/problems/majority-element-ii/ }
*/
package _229_MajorityElementII;
import java.util.ArrayList;
import java.util.List;
/** see test {@link _229_MajorityElementII.SolutionTest} */
public class Solution {
public List<Integer> majorityElement(int[] nums) {
int cand1 = 0;
int cand2 = 1;
int count1 = 0;
int count2 = 0;
for (int num : nums) {
if (num == cand1) {
count1++;
} else if (num == cand2) {
count2++;
} else if (count1 == 0) {
cand1 = num;
count1 = 1;
} else if (count2 == 0) {
cand2 = num;
count2 = 1;
} else {
count1--;
count2--;
}
}
// find answers
count1 = 0;
count2 = 0;
for (int num : nums) {
if (num == cand1) {
count1++;
} else if(num == cand2) {
count2++;
}
}
List<Integer> result = new ArrayList<>();
if (count1 > nums.length / 3) {
result.add(cand1);
}
if (count2 > nums.length / 3) {
result.add(cand2);
}
return result;
}
}