class Solution {
public int longestConsecutive(int[] nums) {
Map<Integer, Integer> map = new HashMap<Integer, Integer>();
Queue<Integer> q = new LinkedList<Integer>();
int max = 0;
for(int n : nums){
q.offer(n);
if(!map.containsKey(n)){
map.put(n, 1);
}else{
map.put(n, map.get(n) + 1);
}
}
while(!q.isEmpty()){
int cur = q.poll();
int len = 1;
int up = cur + 1;
while(map.containsKey(up)){
len ++;
q.remove(up);
up ++;
}
int down = cur - 1;
while(map.containsKey(down)){
len ++;
q.remove(down);
down --;
}
max = Math.max(max, len);
}
return max;
}
}
用hashset做
class Solution {
public int longestConsecutive(int[] nums) {
Set<Integer> s = new HashSet<>();
int max = 0;
for(int n : nums){
s.add(n);
}
for(int i : nums){ //这里loop nums,不是set,要不会有currentmodification错误
int len = 1;
int up = i + 1;
while(s.contains(up)){
len ++;
s.remove(up ++);
}
int down = i - 1;
while(s.contains(down)){
len ++;
s.remove(down --);
}
max = Math.max(max, len);
}
return max;
}
}