LintCode 397.最长上升连续子序列

博客围绕整数数组展开,旨在找出其中的最长上升连续子序列,该序列可从右到左或从左到右。提出使用 O(n) 时间和 O(1) 额外空间解决的挑战,并给出代码思路,用一个循环同时找递增和递减数列,用变量记录长度。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

描述

给定一个整数数组(下标从 0 到 n-1, n 表示整个数组的规模),请找出该数组中的最长上升连续子序列。(最长上升连续子序列可以定义为从右到左或从左到右的序列。)

样例

样例 1:

输入:[5, 4, 2, 1, 3]
输出:4
解释:
给定 [5, 4, 2, 1, 3],其最长上升连续子序列(LICS)为 [5, 4, 2, 1],返回 4。

样例 2:

输入:[5, 1, 2, 3, 4]
输出:4
解释:
给定 [5, 1, 2, 3, 4],其最长上升连续子序列(LICS)为 [1, 2, 3, 4],返回 4。

挑战

使用 O(n) 时间和 O(1) 额外空间来解决

代码部分

public class Solution {
    /**
     * @param A: An array of Integer
     * @return: an integer
     */
    public int longestIncreasingContinuousSubsequence(int[] A) {
        // write your code here
        if(A==null || A.length==0)
        return 0;
        int res1 = 1;//记录当前连续递增序列的规模
        int res2 = 1;//记录当前连续递减序列的规模
        int max1 = 1;//记录连续递增序列的最大规模
        int max2 = 1;//记录连续递减序列的最大规模
        for(int i=1; i<A.length; i++){
            if(A[i] > A[i-1]){
                res1++;
                res2 = 1;
            }
            if(A[i] < A[i-1]){
                res2++;
                res1 = 1;
            }
                max1 = Math.max(max1, res1);
                max2 = Math.max(max2, res2);
        }
        return Math.max(max1, max2);
    }
}

补充说明

 用一个循环,同时寻找递增和递减的数列,使用两个变量res1和res2分别记录最长递增和递减数列的长度,一旦递增递减断开,将对应的计数器重置,而max1与max2是记录最长递增和减序列的长度,防止计数器重置后历史数据消失。

### LintCode 397 题目解析 LintCode 上的第 397 题通常涉及算法设计或数据结构操作。由于具体题目未提供,假设此题可能与字符串处理、数组操作或其他常见算法主题有关。以下是基于典型问题类型的通用解法。 #### 假设题目:整数反转 如果题目要求实现一个函数来反转一个整数,则可以采用如下方法: ```cpp class Solution { public: /** * @param n: an integer to be reversed * @return: the reversed integer */ int reverseInteger(int n) { long res = 0; while (n != 0) { res = res * 10 + n % 10; // 反转逻辑 n /= 10; if (res > INT_MAX || res < INT_MIN) { // 溢出检测 return 0; } } return static_cast<int>(res); } }; ``` 上述代码实现了整数反转功能,并考虑了溢出情况[^6]。 --- #### 假设题目:最小深度二叉树 如果题目要求计算一棵二叉树的最小深度,则可参考以下代码片段: ```cpp class Solution { public: /** * @param root: The root of binary tree. * @return: An integer representing the minimum depth. */ int minDepth(TreeNode* root) { if (!root) return 0; if (!root->left && !root->right) return 1; int leftDepth = root->left ? minDepth(root->left) : INT_MAX; int rightDepth = root->right ? minDepth(root->right) : INT_MAX; return std::min(leftDepth, rightDepth) + 1; } }; ``` 这段代码通过递归方式遍历二叉树节点并返回其最小深度[^2]。 --- #### 假设题目:扁平化嵌套列表 如果题目要求将嵌套列表展平为一维列表,则可以借鉴 Python 的实现思路转换到 C++ 中: ```cpp #include <vector> using namespace std; class NestedIterator { private: vector<int> flattenedList; int index; void flatten(vector<NestedInteger>& nestedList) { for (auto& ni : nestedList) { if (ni.isInteger()) { flattenedList.push_back(ni.getInteger()); } else { flatten(ni.getList()); // 递归展开子列表 } } } public: NestedIterator(vector<NestedInteger> &nestedList) { flatten(nestedList); index = 0; } int next() { return flattenedList[index++]; } bool hasNext() { return index < flattenedList.size(); } }; ``` 以上代码展示了如何利用递归来解决复杂的数据结构问题[^4]。 --- ### 总结 针对不同类型的题目,提供了多种解决方案。无论是整数反转还是更复杂的嵌套列表扁平化,都体现了核心算法思想的应用。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值