[LeetCode] Longest Continuous Increasing Subsequence 最长连续递增序列

Given an unsorted array of integers, find the length of longest continuous increasing subsequence.

Example 1:

Input: [1,3,5,4,7]
Output: 3
Explanation: The longest continuous increasing subsequence is [1,3,5], its length is 3.
Even though [1,3,5,7] is also an increasing subsequence, it's not a continuous one where 5 and 7 are separated by 4. 

Example 2:

Input: [2,2,2,2,2]
Output: 1
Explanation: The longest continuous increasing subsequence is [2], its length is 1. 

Note: Length of the array will not exceed 10,000.

这道题让我们求一个数组的最长连续递增序列,由于有了连续这个条件,跟之前那道Number of Longest Increasing Subsequence比起来,其实难度就降低了很多。我们可以使用一个计数器,如果遇到大的数字,计数器自增1;如果是一个小的数字,则计数器重置为1。我们用一个变量cur来表示前一个数字,初始化为整型最大值,当前遍历到的数字num就和cur比较就行了,每次用cnt来更新结果res,参见代码如下:

解法一:

class Solution {

public:
    int findLengthOfLCIS(vector<int>& nums) {
        int res = 0, cnt = 0, cur = INT_MAX;
        for (int num : nums) {
            if (num > cur) ++cnt;
            else cnt = 1;
            res = max(res, cnt);
            cur = num;
        }
        return res;
    }
};

下面这种方法的思路和上面的解法一样,每次都和前面一个数字来比较,注意处理无法取到钱一个数字的情况,参见代码如下:

解法二:

class Solution {

public:
    int findLengthOfLCIS(vector<int>& nums) {
        int res = 0, cnt = 0, n = nums.size();
        for (int i = 0; i < n; ++i) {
            if (i == 0 || nums[i - 1] < nums[i]) res = max(res, ++cnt);
            else cnt = 1;
        }
        return res;
    }
};

参考资料:

https://discuss.leetcode.com/topic/102999/java-c-clean-solution

本文转自博客园Grandyang的博客,原文链接:[LeetCode] Longest Continuous Increasing Subsequence 最长连续递增序列

,如需转载请自行联系原博主。

时间: 2024-10-30 22:50:34

[LeetCode] Longest Continuous Increasing Subsequence 最长连续递增序列的相关文章

[LeetCode] Number of Longest Increasing Subsequence 最长递增序列的个数

Given an unsorted array of integers, find the number of longest increasing subsequence. Example 1: Input: [1,3,5,4,7] Output: 2 Explanation: The two longest increasing subsequence are [1, 3, 4, 7] and [1, 3, 5, 7]. Example 2: Input: [2,2,2,2,2] Outpu

[LeetCode] Longest Increasing Subsequence

Given an unsorted array of integers, find the length of longest increasing subsequence. For example, Given [10, 9, 2, 5, 3, 7, 101, 18], The longest increasing subsequence is [2, 3, 7, 101], therefore the length is 4. Note that there may be more than

[算法]-Longest Increasing Subsequence

看微软笔试题遇到的. Longest Increasing Subsequence(LIS) means a sequence containing some elements in another sequence by the same order, and the values of elements keeps increasing.For example, LIS of {2,1,4,2,3,7,4,6} is {1,2,3,4,6}, and its LIS length is 5.

[LeetCode] Longest Word in Dictionary 字典中的最长单词

Given a list of strings words representing an English Dictionary, find the longest word in words that can be built one character at a time by other words in words. If there is more than one possible answer, return the longest word with the smallest l

[LeetCode] Longest Univalue Path 最长相同值路径

Given a binary tree, find the length of the longest path where each node in the path has the same value. This path may or may not pass through the root. Note: The length of path between two nodes is represented by the number of edges between them. Ex

找出所有最长连续重复子串及其个数

问题描述: 找出字符串中所以最长连续重复子串及其个数 比如: 输入:123234,最大连续重复字符串为23,个数为2 输入:5555,最大连续重复字符串为555,个数为2 输入:aaabbb 最大连续重复字符串为aa,个数为2:和bb,个数为2 必须存在重复的字符串才算,只出现一次的不算.可能存在多个相同长度的不同字符串,比如aaabbb. 解题思路 与[求一个字符串中连续出现次数最多的子串]的区别体现在两个方面:一是要找最长子串(重复次数大于等于2即可):二是要考虑子串是有重叠的重复,如eee

JavaScript实现找出数组中最长的连续数字序列_javascript技巧

原始题目: 给定一个无序的整数序列, 找最长的连续数字序列. 例如: 给定[100, 4, 200, 1, 3, 2], 最长的连续数字序列是[1, 2, 3, 4]. 小菜给出的解法: function maxSequence(array,step){ var _array = array.slice(), //clone array _step = 1, _arrayTemp = [], i = 0; var parseLogic = { //result container parseRe

【字符串处理算法】最长连续字符及其出现次数的算法设计及C代码实现

一.需求描述 输入一个字符串,编写程序找出这个字符串中的最长连续字符,并求出其连续出现的次数. 例如,"123444445"中的最长连续字符是4,其连续出现的次数为5:"abcddef"中的最长连续字符是d,其连续出现的次数为2:"ab"中的最长连续字符是a,其连续出现的次数为1.   二.算法设计 我们可以采取逐个比较字符串中各个字符的办法来获取最长连续字符及其连续出现的次数.程序的总体流程如图1所示. 图1 程序的总体流程   三.特殊流程考

返回给定字符串中最长连续数字串

/* 不用任何库函数,系统函数, 完成函数 int maxContinuNum(const char* inputstr,char *outputstr); 返回给定字符串中最长连续数字串,让outputstr指向该串,然后值是其长度. 例如sss12345ss1245sfdf123456789返回,9,outputstr指向123456789. */ #include <iostream> using namespace std; int maxContunuNum(const char*