[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 ...
随机推荐
- oracle 11g数据库 DMP还原数据库
-------------------------- jd :表空间 -------------------------- --本地登陆 cmd下直接执行 sqlplus/as sysdba; --修 ...
- KVM之十:虚拟机在线添加网卡
1.查看原有网卡信息 #virsh domiflist snale Interface Type Source Model MAC ---------------------------------- ...
- 设计模式之迭代器模式详解(foreach的精髓)
作者:zuoxiaolong8810(左潇龙),转载请注明出处,特别说明:本博文来自博主原博客,为保证新博客中博文的完整性,特复制到此留存,如需转载请注明新博客地址即可. 各位好,很久没以LZ的身份和 ...
- 开源小工具 酷狗、网易音乐缓存文件转mp3工具
发布一个开源小工具,支持将酷狗和网易云音乐的缓存文件转码为MP3文件. 以前写过kgtemp文件转mp3工具,正好当前又有网易云音乐缓存文件需求,因此就在原来小工具的基础上做了一点修改,增加了对网易云 ...
- 关于SELECT LAST_INSERT_ID()的使用规则
尊重个人劳动成果,转载请注明出处: http://blog.csdn.net/czd3355/article/details/71302441 首先我先解释以下在在映射文件中的代码是什么意思. < ...
- 在Python中使用Redis
在Python中要使用Redis数据库,首先要安装redis 之前的博客中有写到在命令行模式下操作Redis数据库. 要在项目中使用的话可以这么做: 通过初始化 redis.Redis,得到返回的对象 ...
- Linux学习--进程概念
>>进程 说进程,感觉好空洞,来一张图,Linux下的进程: ps -eo pid,comm,cmd 说明:-e表示列出全部进程,-o pid,comm,cmd表示我们需要PID,COMM ...
- SAN LUN Mapping出错导致文件系统共享冲突,数据恢复成功
[用户单位] 中国联通某分公司[数据恢复故障描述] SUN 光纤存储系统,中心存储为6枚300G硬盘组成的RAID6,划分为若干LUN,MAP到不同业务的服务器上,服务器上运行SUN SOLAR ...
- split 过滤空的元素
命令形式: split(str='',number=string.count(str))[n] str 分隔符 number 切分几次,[n] 获取第几个值. 1.如果切分的可迭代对象中包含空元素的解 ...
- 多线程里面的关键字,wait, notfiy, 锁(synchronized), lock接口
多线程环境下,必须考虑线程同步的问题,这是因为多个线程同时访问变量或者资源时会有线程争用,比如A线程读取了一个变量,B线程也读取了这个变量,然后他们同时对这个变量做了修改,写回到内存中,由于是同时做修 ...