You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
25 lines
582 B
25 lines
582 B
package class008;
|
|
|
|
import java.util.HashMap;
|
|
|
|
public class Code02_LongestConsecutiveSequence {
|
|
|
|
public static int longestConsecutive(int[] nums) {
|
|
HashMap<Integer, Integer> map = new HashMap<>();
|
|
int len = 0;
|
|
for (int num : nums) {
|
|
if (!map.containsKey(num)) {
|
|
map.put(num, 1);
|
|
int preLen = map.containsKey(num - 1) ? map.get(num - 1) : 0;
|
|
int posLen = map.containsKey(num + 1) ? map.get(num + 1) : 0;
|
|
int all = preLen + posLen + 1;
|
|
map.put(num - preLen, all);
|
|
map.put(num + posLen, all);
|
|
len = Math.max(len, all);
|
|
}
|
|
}
|
|
return len;
|
|
}
|
|
|
|
}
|