-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path128.java
33 lines (33 loc) · 1.1 KB
/
128.java
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
class Solution {
public int longestConsecutive(int[] nums) {
Map<Integer,Integer> map = new HashMap<>();
for(int num : nums){
if(map.containsKey(num)) continue;
boolean l = map.containsKey(num-1);
boolean r = map.containsKey(num+1);
if(l&&r){
int ll = map.get(num-1);
int rr = map.get(num+1);
map.put(num-ll,ll+rr+1);
map.put(num+rr,ll+rr+1);
map.put(num,1);
}else if((!l)&&r){
int rr = map.get(num+1);
map.put(num+rr,rr+1);
map.put(num,rr+1);
}else if(l&&(!r)){
int ll = map.get(num-1);
map.put(num-ll,ll+1);
map.put(num,ll+1);
}else{
map.put(num,1);
}
}
int max = 0;
for(Map.Entry<Integer,Integer> entry : map.entrySet()){
//System.out.printf("%d %d\n",entry.getKey(),entry.getValue());
if(entry.getValue()>max) max=entry.getValue();
}
return max;
}
}