【nc】 Arrays&Hashing 8/9 Longest Consecutive Sequence 最长连续序列 128

===

思路:

1. 比较容易想到的是使用map先标记一遍,然后遍历数组中的每一个去找,这样会超时,所以使用方法2,set

2.使用set, set = new Set(nums)

然后遍历

if(!set.has(c-1)){

  while(set.has(c+1)){

    len = len+1

    c = c+1

  }

}

计算maxLen,最终返回

 

posted @ 2023-06-24 05:01  hh9515  阅读(13)  评论(0)    收藏  举报