Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Solution {
- public int longestConsecutive(int[] nums) {
- HashSet<Integer> set = new HashSet<>();
- for(int i: nums) set.add(i);
- int maxStreak = 0;
- for(int i: nums){
- int curStreak = 1;
- if(!set.contains(i-1)) {
- int j = i+1;
- while(set.contains(j)) {
- curStreak ++;
- j ++;
- }
- maxStreak = Math.max(curStreak, maxStreak);
- }
- }
- return maxStreak;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment