✉️문제
https://leetcode.com/problems/longest-consecutive-sequence/
🗝 문제풀이
class Solution {
public int longestConsecutive(int[] nums) {
if(nums == null) return 0;
Set<Integer> set = new HashSet<>();
for(int n : nums) set.add(n);
int answer = 0;
for(int n : set) {
int length = 1;
if(!set.contains(n - 1)) {
while(set.contains(n + length)) {
length++;
}
answer = Math.max(answer, length);
}
}
return answer;
}
}