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