思路:先将所有数放进HashMap中,然后就,
遍历Hashmap,挨个判断连续最大值
class Solution {
public int longestConsecutive(int[] nums) {
int max = 0;
Map<Integer, Integer>map = new HashMap<>();
for(int num : nums)
{
map.put(num,num);
}
for (Map.Entry<Integer, Integer> entry : map.entrySet())
{
max = Math.max(max, jia(map,entry.getKey()) + jian(map ,entry.getKey()) + 1);
}
return max;
}
public int jia(Map a, int b)
{
int num = 0;
while(a.containsKey(b + 1))
{
b ++;
num ++;
}
return num;
}
public int jian(Map a, int b)
{
int num = 0;
while(a.containsKey(b - 1))
{
b --;
num ++;
}
return num;
}
}