simple, use hashMap


  • 0

    class Solution {
    public int longestConsecutive(int[] nums) {
    if(0==nums.length)return 0;
    Map<Integer,Integer> s = new HashMap<>();
    int res=Integer.MIN_VALUE;
    for(int e:nums){
    if(!s.containsKey(e)){
    int left= (s.containsKey(e-1))? s.get(e-1):0;
    int right=(s.containsKey(e+1))? s.get(e+1):0;
    int t=left+right+1;
    res=Math.max(t,res);
    s.put(e,t);
    s.put(e-left,t);
    s.put(e+right,t);
    }
    }
    return res;
    }
    }


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.