-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlc-697.java
More file actions
42 lines (40 loc) · 1.73 KB
/
lc-697.java
File metadata and controls
42 lines (40 loc) · 1.73 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
class Solution {
public int findShortestSubArray(int[] nums) {
Map<Integer, List<Integer>> m = new HashMap();
int max = 0;//出现次数最大值
int[] min_D = {-1, Integer.MAX_VALUE};
if(nums.length == 0 || nums.length == 1)return nums.length;
for(int i = 0; i<nums.length; i++) {
if(m.containsKey(nums[i])) {
List<Integer> instance = m.get(nums[i]);
instance.add(i);
if(instance.size() >= max) {
//等于max也要考虑,因为可能最短距离会被刷新
int d = instance.get(instance.size()-1) - instance.get(0);
//System.out.println(i + " " + nums[i] + " " + instance + " " + min_D[0] + " " + min_D[1] + " " + d);
if(min_D[0] == nums[i]) {
min_D[1] = d;
}else {
//如果不是同一元素,则检查最短距离是否更新
if(instance.size() > max || d < min_D[1]) {
//System.out.println("haha");
min_D[1] = d;
min_D[0] = nums[i];
}
}
if(min_D[0] == -1) {
min_D[1] = d;
min_D[0] = nums[i];
}
max = instance.size();
}
}else {
List<Integer> l = new ArrayList();
l.add(i);
m.put(nums[i], l);
}
}
if(min_D[0] == -1) return 1;//说明所有的元素没有重复
return min_D[1] + 1;
}
}