题目描述:

解析:hashSet作记录 从左往右搜 从右往左搜
class Solution {public int longestConsecutive(int[] nums) {HashSet<Integer> set = new HashSet<>();int longest = 0; //记录长度for (int num : nums) {set.add(num);}for (int num : nums) {//从左往右搜 例如100,搜索101,102,103....int currNum = num;int currLong = 1;while (set.remove(currNum + 1)) currNum++;currLong += currNum - num;//从右往左搜 例如100,搜索99,98,97....currNum = num;while (set.remove(currNum - 1)) currNum--;currLong += num - currNum;longest = Math.max(longest, currLong);}return longest;}}
