-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLeetCode169.java
More file actions
40 lines (37 loc) · 1.2 KB
/
LeetCode169.java
File metadata and controls
40 lines (37 loc) · 1.2 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
import java.util.HashMap;
public class LeetCode169 {
public static void main(String[] args) {
// 输入:nums = [3,2,3]
// 输出:3
System.out.println(new Solution169().majorityElement(new int[] { 3, 2, 3 }));
// 输入:nums = [2,2,1,1,1,2,2]
// 输出:2
System.out.println(new Solution169().majorityElement(new int[] { 2, 2, 1, 1, 1, 2, 2 }));
}
}
class Solution169 {
public int majorityElement(int[] nums) {
int length = nums.length;
int half = length / 2 + length % 2;
HashMap<Integer, Integer> map = new HashMap<>();
for (int i = 0; i < nums.length; i++) {
if (map.containsKey(nums[i])) {
if (map.get(nums[i]) + length - i >= half) {
map.put(nums[i], map.get(nums[i]) + 1);
} else {
map.remove(nums[i]);
}
} else {
if (length - i >= half) {
map.put(nums[i], 1);
}
}
}
for (Integer item : map.keySet()) {
if (map.get(item) >= half) {
return item.intValue();
}
}
return -1;
}
}