最长连续序列

题目

最长连续序列
给定一个未排序的整数数组,找出最长连续序列的长度。

要求算法的时间复杂度为 O(n)。

示例:

输入: [100, 4, 200, 1, 3, 2]
输出: 4
解释: 最长连续序列是 [1, 2, 3, 4]。它的长度为 4。

我的思路

1、快速排序,遍历数组,使用计数器进行计数。
注意:数组中的重复数字
2、参考博客Grandyang,使用set。

代码

//
// Created by HINTS on 2018/12/20.
//

#include <iostream>
#include <vector>
#include <c++/algorithm>
#include <unordered_set>
using  namespace std;
/*
//第一种解法:使用STL
int findKthLargest1(vector<int>& nums, int k) {
    sort(nums.begin(), nums.end());
    return nums[nums.size() - k];
}

//第二种解法:自己实现快速排序
//返回p, 使得nums[l...p-1] < nums[p] ; nums[p+1...r] > nums[p]
int __partition(vector<int> &nums, int l , int r){
    int v = nums[l];
    int j = l;
    for (int i = l + 1; i <= r; ++i) {
        if(nums[i] < v){
            j++;
            swap(nums[j], nums[i]);
        }
    }
    swap(nums[l], nums[j]);
    return j;
}

void __quickSort(vector<int> &nums, int l, int r){
    if(l >= r)
        return ;
    int p = __partition(nums, l, r);
    __quickSort(nums, l, p-1);
    __quickSort(nums, p+1, r);
}
void quickSort(vector<int> &nums, int n){
    __quickSort(nums,0 , n-1);
}

int longestConsecutive(vector<int> &nums){
    if(nums.empty())
        return 0;
    quickSort(nums, nums.size());
    int maxLength = 0;
    int count = 1;
    for (int i = 0; i < nums.size() - 1; ++i) {
        if(nums[i+1] == nums[i])
            continue;
        if(nums[i + 1] - nums[i] == 1){
            count++;
            maxLength = max(maxLength, count);
        }else{
            count = 1;
        }

    }
    return max(maxLength, count);
}
 */
int longestConsecutive(vector<int> &nums){
    int res = 0;
    unordered_set<int>s (nums.begin(), nums.end());
    for(int val : nums){
        if(!s.count(val))
            continue;
        s.erase(val);
        int pre = val - 1, next = val + 1;
        while (s.count(pre))
            s.erase(pre--);
        while (s.count(next))
            s.erase(next++);
        res = max(res,next - pre - 1);
    }
    return res;
}
int main(){
    vector<int> nums = {100, 4, 200, 1, 3, 2};
    int res = longestConsecutive(nums);
    cout << res << endl;
    return 0;
}
在LeetCode(力扣)上,有一个经典的算法题目叫做“最长连续序列”(Longest Continuous Increasing Subsequence),通常用C++或其他编程语言进行解答。这个题目的目的是找到一个给定整数数组中的最长递增子序列。递增子序列是数组中的一段连续元素,它们按顺序严格增大。 这里是一个简单的C++解决方案思路: ```cpp #include <vector> using namespace std; class Solution { public: int longestContinuousIncreasingSubsequence(vector<int>& nums) { if (nums.empty()) return 0; // 避免空数组的情况 int n = nums.size(); vector<int> dp(n, 1); // dp[i] 表示以nums[i]结尾的最长递增子序列长度 int max_len = 1; // 初始化最长递增子序列长度为1 for (int i = 1; i < n; ++i) { // 遍历数组,从第二个元素开始 if (nums[i] > nums[i-1]) { // 如果当前元素比前一个大 dp[i] = dp[i-1] + 1; // 更新dp值,考虑加入当前元素后的增长长度 max_len = max(max_len, dp[i]); // 检查是否更新了最长序列长度 } } return max_len; // 返回最长连续递增子序列的长度 } }; ``` 在这个代码中,我们使用了一个动态规划(Dynamic Programming)的方法,维护了一个数组`dp`来存储每个位置以该位置元素结尾的最大递增子序列长度。遍历过程中,如果遇到当前元素大于前一个元素,则说明可以形成一个新的递增子序列,所以将`dp[i]`设置为`dp[i-1]+1`,并更新全局的最长序列长度。 如果你想要深入了解这个问题,可以问: 1. 这个问题的时间复杂度是多少? 2. 动态规划是如何帮助解决这个问题的? 3. 如何优化这个算法,使其空间复杂度更低?
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值