哈希表
public int longestConsecutive(int[] nums) {Set<Integer> set = new HashSet<>();for (int num : nums) {set.add(num);}int maxLen = 0;for (int n : set) {// 以当前数开头的序列if (!set.contains(n - 1)) {int currentlen = 1;while (set.contains(n + 1)) {n += 1;currentlen++;}maxLen = Math.max(maxLen, currentlen);}// 若set中包含你前面一个数,说明轮到不你来开头}return maxLen;}
