-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLeetCode594.java
More file actions
64 lines (59 loc) · 1.9 KB
/
LeetCode594.java
File metadata and controls
64 lines (59 loc) · 1.9 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
import java.util.HashMap;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Arrays;
public class LeetCode594 {
public static void main(String[] args) {
// 输入:nums = [1,3,2,2,5,2,3,7]
// 输出:5
System.out.println(new Solution594_2().findLHS(new int[] { 1, 3, 2, 2, 5, 2, 3, 7 }));
// 输入:nums = [1,2,3,4]
// 输出:2
System.out.println(new Solution594_2().findLHS(new int[] { 1, 2, 3, 4 }));
// 输入:nums = [1,1,1,1]
// 输出:0
System.out.println(new Solution594_2().findLHS(new int[] { 1, 1, 1, 1 }));
}
}
class Solution594_1 {
// NOTE: 哈希表 + 记数
public int findLHS(int[] nums) {
HashMap<Integer, Integer> map = new HashMap<>();
for (int i = 0; i < nums.length; i++) {
map.put(nums[i], map.getOrDefault(nums[i], 0) + 1);
}
if (map.size() > 1) {
// 可以找到两个
ArrayList<Integer> keys = new ArrayList<>(map.keySet());
Collections.sort(keys);
int result = 0;
for (int i = 0; i < keys.size() - 1; i++) {
if (keys.get(i) + 1 == keys.get(i + 1)) {
result = Math.max(result, map.get(keys.get(i)) + map.get(keys.get(i + 1)));
}
}
return result;
} else {
// 只能找到一个
return 0;
}
}
}
class Solution594_2 {
// NOTE: 排序 + 滑动窗口
public int findLHS(int[] nums) {
Arrays.sort(nums);
int begin = 0;
int res = 0;
int len = nums.length;
for (int i = 0; i < len; i++) {
while ((nums[i] - nums[begin]) > 1) {
begin++;
}
if ((nums[i] - nums[begin]) == 1) {
res = Math.max(res, i - begin + 1);
}
}
return res;
}
}