【Leetcode】128. Longest Consecutive Sequence

1 对于nums中的每一个num,如果num-1存在,且长度是left,num+1存在,且长度是right,则新的长度就是left+right+1;通过dic.get函数,如果num-1不存在,就默认left是0,同理num+1也是一样的;所以定义的dic中,key是每个num,对应的value是长度

2 同时需要定义个全局变量来寻找最长的length

3 最后要更新新形成的sequence的最左边和最右边的值,也就是dic[num-left]和dic[num+right]


We will use HashMap. The key thing is to keep track of the sequence length and store that in the boundary points of the sequence. For example, as a result, for sequence {1, 2, 3, 4, 5}, map.get(1) and map.get(5) should both return 5.



你可能感兴趣的:(【Leetcode】128. Longest Consecutive Sequence)