1. 将数组中元素全部放入set集合;去重

  2. 对于set集合中的元素遍历,对于每一个元素num,首先判断num-1是否在集合里

  3. 若在,说明该元素不可能足为子序列的起始,则判断下一个元素

  4. 若不在,则将其作为子序列起始位,判断以其为首的子序列长度

  5. 比较得到最长的连续子序列长度。

    public int MLS (int[] arr) {
         HashSet<Integer> set = new HashSet();
    
         //1. 去重
         for(int num : arr){
             set.add(num);
         }
    
         int res = 0;
    
         //2. 遍历
         for(int num : set){
    
             if(set.contains(num - 1)){
                 //3. 不是起始位,判断下一个
                 continue;
             }else{
                 //4.是起始位,求序列长度
                 int count = 0;
                 while(set.contains(num++)){
                     count++;
                 }
                 res = Math.max(res,count);
             }
         }
    
         return res;
     }