[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 = , 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
参考资料:
https://leetcode.com/problems/longest-continuous-increasing-subsequence/
LeetCode All in One 题目讲解汇总(持续更新中...)
[LeetCode] Longest Continuous Increasing Subsequence 最长连续递增序列的更多相关文章
- [LeetCode] 674. Longest Continuous Increasing Subsequence 最长连续递增序列
Given an unsorted array of integers, find the length of longest continuous increasing subsequence. E ...
- LeetCode 674. Longest Continuous Increasing Subsequence最长连续递增序列 (C++/Java)
题目: Given an unsorted array of integers, find the length of longest continuous increasing subsequenc ...
- Leetcode674.Longest Continuous Increasing Subsequence最长连续递增序列
给定一个未经排序的整数数组,找到最长且连续的的递增序列. 示例 1: 输入: [1,3,5,4,7] 输出: 3 解释: 最长连续递增序列是 [1,3,5], 长度为3. 尽管 [1,3,5,7] 也 ...
- 674. Longest Continuous Increasing Subsequence最长连续递增子数组
[抄题]: Given an unsorted array of integers, find the length of longest continuous increasing subseque ...
- LeetCode Longest Continuous Increasing Subsequence
原题链接在这里:https://leetcode.com/problems/longest-continuous-increasing-subsequence/description/ 题目: Giv ...
- LeetCode 674. Longest Continuous Increasing Subsequence (最长连续递增序列)
Given an unsorted array of integers, find the length of longest continuous increasing subsequence. E ...
- LeetCode 674. 最长连续递增序列(Longest Continuous Increasing Subsequence) 18
674. 最长连续递增序列 674. Longest Continuous Increasing Subsequence 题目描述 给定一个未经排序的整型数组,找到最长且连续的递增序列. Given ...
- C#LeetCode刷题之#674-最长连续递增序列( Longest Continuous Increasing Subsequence)
问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3734 访问. 给定一个未经排序的整数数组,找到最长且连续的的递增 ...
- [Swift]LeetCode674. 最长连续递增序列 | Longest Continuous Increasing Subsequence
Given an unsorted array of integers, find the length of longest continuous increasing subsequence (s ...
随机推荐
- java.lnag.Throwable详细解读
public class Throwable extends Object implemnts Serializable Throwable类是所有错误或异常的超类.只有当对象是此类(或其中之 ...
- Java基础学习笔记二十八 管家婆综合项目
本项目为JAVA基础综合项目,主要包括: 熟练View层.Service层.Dao层之间的方法相互调用操作.熟练dbutils操作数据库表完成增删改查. 项目功能分析 查询账务 多条件组合查询账务 添 ...
- 安装/或更新node和npm
1.安装/或更新node和npm 安装node: node 的官网 nodejs.org 去下载 msi 安装包 更新npm: 使用命令 npm install npm@latest ...
- 201621123035 《Java程序设计》第1周学习总结
1.本周学习总结 本周学习内容:Java平台概论.认识JDK规范与操作.了解JVM.JRE与JDK.撰写Java原始码.path是什么 关键词:JVM.JRE.JDK 联系:JVM是Java虚拟机的缩 ...
- 【学习笔记】windows安装jhipster踏坑记录
序: 入职新公司第二天了,本来第一天是配置环境来着,配了一下午也没搞成那个jhipster的安装,每次以为应该正常的时候都是不对,yo是yeoman的指令,但是我是使用yarn管理的yeoman 纠结 ...
- 利用封装、继承对Java代码进行优化
注:本文实例分别可以在oldcastle(未优化的代码)和newcastle(优化后的代码)中查看,网址见文末 城堡游戏: 城堡中有多个房间,用户通过输入north, south, east, wes ...
- LR之error(一)
1 录制时频繁卡死的解决方案 添加数据保护 路径:计算机--高级系统设置(环境变量设置的上级窗口)--高级--设置--数据执行保护 更改LR录制设置,将run-time setting的brower改 ...
- 常用的汇编指令 movs stos
movsb 把寄存机esi所存的地址的数据以字节复制到edi movsw 把寄存机esi所存的地址的数据以word复制到edi movsd 把寄存机esi所存的地址的数据以dword复制到e ...
- MySQL binlog 日志
一:MySQL 日志的三种类型: statement.row.mix 格式.推荐使用row格式. 怎么设置自己的日志格式呢? 1. set globle binlog_format='MIXED' 2 ...
- Python内置函数(18)——bin
英文文档: bin(x) Convert an integer number to a binary string. The result is a valid Python expression. ...