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 = , cnt = , cur = INT_MAX;
for (int num : nums) {
if (num > cur) ++cnt;
else cnt = ;
res = max(res, cnt);
cur = num;
}
return res;
}
};

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

解法二:

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

Github 同步地址:

https://github.com/grandyang/leetcode/issues/674

类似题目:

Number of Longest Increasing Subsequence

Minimum Window Subsequence

参考资料:

https://leetcode.com/problems/longest-continuous-increasing-subsequence/

https://leetcode.com/problems/longest-continuous-increasing-subsequence/discuss/107352/Java-code-6-liner

https://leetcode.com/problems/longest-continuous-increasing-subsequence/discuss/107365/JavaC%2B%2BClean-solution

LeetCode All in One 题目讲解汇总(持续更新中...)

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

  1. LeetCode 674. Longest Continuous Increasing Subsequence最长连续递增序列 (C++/Java)

    题目: Given an unsorted array of integers, find the length of longest continuous increasing subsequenc ...

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

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

  3. 674. Longest Continuous Increasing Subsequence最长连续递增子数组

    [抄题]: Given an unsorted array of integers, find the length of longest continuous increasing subseque ...

  4. Leetcode674.Longest Continuous Increasing Subsequence最长连续递增序列

    给定一个未经排序的整数数组,找到最长且连续的的递增序列. 示例 1: 输入: [1,3,5,4,7] 输出: 3 解释: 最长连续递增序列是 [1,3,5], 长度为3. 尽管 [1,3,5,7] 也 ...

  5. LeetCode 674. Longest Continuous Increasing Subsequence (最长连续递增序列)

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

  6. [Leetcode]674. Longest Continuous Increasing Subsequence

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

  7. leetcode300. Longest Increasing Subsequence 最长递增子序列 、674. Longest Continuous Increasing Subsequence

    Longest Increasing Subsequence 最长递增子序列 子序列不是数组中连续的数. dp表达的意思是以i结尾的最长子序列,而不是前i个数字的最长子序列. 初始化是dp所有的都为1 ...

  8. 【Leetcode_easy】674. Longest Continuous Increasing Subsequence

    problem 674. Longest Continuous Increasing Subsequence solution class Solution { public: int findLen ...

  9. 【LeetCode】674. Longest Continuous Increasing Subsequence 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 动态规划 空间压缩DP 日期 题目地址:https: ...

随机推荐

  1. telnet: Unable to connect to remote host: Connection refused

    问题描述: telnet: Unable to connect to remote host: Connection refused 已解决,需要安装telent 服务,请查看下方的链接文章: htt ...

  2. [IDA] Oops! internal error 40343 occured.

    问题描述: 解决方案: 安装 IDA 时,其路径下不要出现中文.  

  3. Oracle 查询练习

    非常经典的一些日常醒脑练习内容!! 如有更高效的写法欢迎赐教! .已知Oracle的Scott用户中提供了三个测试数据库表,名称分别为dept,emp和salgrade.使用SQL语言完成以下操作 ) ...

  4. Winform中设置ZedGraph的曲线为散点图

    场景 Winform中设置ZedGraph的曲线符号Symbol以及对应关系: https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/details/10 ...

  5. IIS创建文件服务器(WebDAV)

    1.安装IIS,选择安装WEBDAV组件.然后新建站点,站点目录不需要额外设置任何权限 安装完成后组件: 2.配置WebDAV: 添加创作规则:允许某用户写入,其他所有用户读取.(写入规则必须要放在第 ...

  6. golang多个项目时如何配置GOPATH,使用gb包依赖管理工具,不同项目配置不同的GOPATH的

    golang多个项目时如何配置GOPATH,使用gb包依赖管理工具,不同项目配置不同的GOPATH的 1:执行脚本setGoPath.sh#!/bin/bashif [[ $GOPATH =~ .*$ ...

  7. AT+CNUM获取不到手机号

    原因是卡商没有写入SIM卡 解决办法 手动写入 1. 先确认SIM卡的本机号码 2. 选择电话本存储 /* AT+CPBS Select phonebook memory storage " ...

  8. 如何在Ubuntu编译ice

    摘要:所有操作都在root用户下执行 第一步:下载ice最新版本,以3.7版本为例 第二步:安装ICE依赖包里面的三方库 #apt-get install libmcpp-dev;apt-get in ...

  9. RDD源码分析

    RDD源码解析 一. RDD.scala - Resilient Distributed Dataset (RDD) 弹性分布式数据集 弹性: 体现在计算上面 - the basic abstract ...

  10. SQL Server学习内容(一)

    SQL Server SQL Server对大小写不敏感,每条语句末端使用分号. 1.SQL命令 SELECT 从数据中提取数据 UPDATE 更新数据中的数据 DELETE 从数据库中删除数据 IN ...