最长连续序列

复制代码
class Solution 
{
public:
    int longestConsecutive(vector<int>& nums)
     {
        sort(nums.begin(), nums.end());
//  for (int n : nums)
//      cout << n << " ";
//  cout << endl;

 int length = nums.size();
 int max = 0;
 if(length==0||length==1) return length;
 for (int begin = 0; begin < length-1; begin++)
 {
     int l = 1;
     int i = begin;
     for (int j = begin + 1; j < length; j++)
     {
         if ((nums[i] + 1 )== nums[j] )
         {
             //cout << i << " " << j << " " << l << " " << nums[i] << " " << nums[j] << endl;
             l++;
             i++;
            // j++; j已经+了没必要+
         }
           else if (nums[i] == nums[j])
  {
      i++;
  }
         else break;
     }
     if (max < l) max = l;
 }
return max;


    }
};

依旧复杂度问题

复制代码
class Solution 
{
public:
    int longestConsecutive(vector<int>& nums)
     {
        //    if (nums.empty()) return 0; // 处理空数组,避免后续访问越界
        
        // // 用哈希集合去重并加速查找
        // unordered_set<int> numSet(nums.begin(), nums.end());
    int length = nums.size();
        sort(nums.begin(), nums.end());
    // 处理特殊情况
    if (length <= 1) 
    {
        return length;
    }
    int max_len = 1;  // 最长连续序列长度,至少为1
    int current_len = 1;  // 当前连续序列长度
    // 遍历数组,从第二个元素开始
    for (int i = 1; i < length; i++)
     {
        // 如果当前元素与前一个元素相同,不增加长度
        if (nums[i] == nums[i - 1]) 
        {
            continue;
        }
        // 如果当前元素是前一个元素+1,说明连续
        else if (nums[i] == nums[i - 1] + 1) 
        {
            current_len++;
            // 更新最长长度
            if (current_len > max_len) 
            {
                max_len = current_len;
            }
        }
        // 序列中断,重置当前长度
        else 
        {
            current_len = 1;
        }
    }
    return max_len;
     }
 
};

class Solution 
{
public:
    int longestConsecutive(vector<int>& nums)
     {
 sort(nums.begin(), nums.end());
 int length = nums.size();
 int max = 1;
 if (length == 0 || length == 1) return length;
 int begin = 0;
 int l = 1;
         if (length == 0 || length == 1) return length;
         for (int begin = 0; begin < length - 1; begin++)
         {
           
             if (nums[begin] + 1 == nums[begin + 1])
             {
                 l++;
                 if (max < l) max = l;
             }
//之前一直是两个if 会导致else会被错误执行
             else if (nums[begin] == nums[begin + 1]) continue;
                 else
                 {
                     if (max < l) max = l;
                     l = 1;
                 }
             
         }
         return max;
     }
};

更改的重点是将双重循环改为了单循环

老版本是暴力求结果

新版本是将这一个和前一个进行对比,如果相等就再+1,如果刚刚好大于1就长度+1

相关推荐
im_AMBER1 小时前
算法笔记 18 二分查找
数据结构·笔记·学习·算法
C雨后彩虹1 小时前
机器人活动区域
java·数据结构·算法·华为·面试
MarkHD1 小时前
车辆TBOX科普 第53次 三位一体智能车辆监控:电子围栏算法、驾驶行为分析与故障诊断逻辑深度解析
算法
Gomiko2 小时前
C/C++基础(四):运算符
c语言·c++
苏小瀚2 小时前
[算法]---路径问题
数据结构·算法·leetcode
freedom_1024_2 小时前
【c++】使用友元函数重载运算符
开发语言·c++
zmzb01032 小时前
C++课后习题训练记录Day43
开发语言·c++
月明长歌2 小时前
【码道初阶】一道经典简单题:多数元素(LeetCode 169)|Boyer-Moore 投票算法详解
算法·leetcode·职场和发展
wadesir2 小时前
C语言模块化设计入门指南(从零开始构建清晰可维护的C程序)
c语言·开发语言·算法
t198751282 小时前
MATLAB水声信道仿真程序
开发语言·算法·matlab