[LeetCode] 674. 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] 674. Longest Continuous Increasing Subsequence 最长连续递增序列的更多相关文章
- LeetCode 674. Longest Continuous Increasing Subsequence最长连续递增序列 (C++/Java)
题目: Given an unsorted array of integers, find the length of longest continuous increasing subsequenc ...
- [LeetCode] Longest Continuous Increasing Subsequence 最长连续递增序列
Given an unsorted array of integers, find the length of longest continuous increasing subsequence. E ...
- 674. Longest Continuous Increasing Subsequence最长连续递增子数组
[抄题]: Given an unsorted array of integers, find the length of longest continuous increasing subseque ...
- Leetcode674.Longest Continuous Increasing Subsequence最长连续递增序列
给定一个未经排序的整数数组,找到最长且连续的的递增序列. 示例 1: 输入: [1,3,5,4,7] 输出: 3 解释: 最长连续递增序列是 [1,3,5], 长度为3. 尽管 [1,3,5,7] 也 ...
- 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
Given an unsorted array of integers, find the length of longest continuous increasing subsequence. E ...
- leetcode300. Longest Increasing Subsequence 最长递增子序列 、674. Longest Continuous Increasing Subsequence
Longest Increasing Subsequence 最长递增子序列 子序列不是数组中连续的数. dp表达的意思是以i结尾的最长子序列,而不是前i个数字的最长子序列. 初始化是dp所有的都为1 ...
- 【Leetcode_easy】674. Longest Continuous Increasing Subsequence
problem 674. Longest Continuous Increasing Subsequence solution class Solution { public: int findLen ...
- 【LeetCode】674. Longest Continuous Increasing Subsequence 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 动态规划 空间压缩DP 日期 题目地址:https: ...
随机推荐
- 三、ForkJoin分析
ForkJoin分析 一.ForkJoin ForkJoin是由JDK1.7后提供多线并发处理框架.ForkJoin的框架的基本思想是分而治之.什么是分而治之?分而治之就是将一个复杂的计算,按照设 ...
- 博客中新浪图床 迁移至 阿里云的OSS
前言 因为之前有个新浪的图床,还挺好用,而且免费,自己博客的图片上传到其上面也挺方便的,但是,前几周吧,突然图片就不能访问了,之前本来是想通过添加 meta 头来解决的,但是发现没有效果.于是就自己搞 ...
- linux 查找被删除但是未被释放空间的文件 并释放资源
使用du -sh 和df -h 查看/data目录,发现结果相差一半,后来了解到: 使用rm命令删除文件时,只有当该文件不存在任何link才会被删除 当有进程访问这个文件时,这个文件的实际占用空间就 ...
- eclipse快速给表达式生成对应变量的快捷键
这里记录下在Eclipse中快速给表达式生成对应变量的快捷键,有两种方式. [Ctrl + 2] 光标放在该表达式行的任意位置,按[Ctrl+2],会弹出提示,根据提示选择[F/L/R],就会自动生成 ...
- Spring Boot自定义配置实现IDE自动提示
一.背景 官方提供的spring boot starter的配置项,我们用IDE配置的时候一般都有自动提示的,如下图所示 而我们自己自定义的配置却没有,对开发非常不友好容易打错配置,那这个是怎样实现的 ...
- WPF,ComboBox,取汉字首字母,extBoxBase.TextChanged
1取汉字汉语拼音首字母: private static string GetFirstLetterOfChineseString(string CnChar) { long iCnChar; byte ...
- 拒绝CPU挖矿矿工有责
长期以来CPU挖矿给挖矿行业带来持久的负面影响,因为CPU是电脑的核心设备,一挖矿就干不了别的了,大家是否可以达成共识拒绝CPU挖矿? 显卡挖矿刚好构建在不影响大众的日常工作生活对电脑的需求之上,家用 ...
- NET 特性(Attribute)
NET 特性(Attribute) 转自 博客园(Fish) 特性(Attribute):是用于在运行时传递程序中各种元素(比如类.方法.结构.枚举.组件等)的行为信息的声明性标签. 您可以通过使用特 ...
- ASP.NET MVC EF 连接数据库(一)-----Database First
database first (VS2015 ,Sql Server2014) 1,新建MVC项目 实例: 源码代码:http://note.youdao.com/noteshare?id=1fd ...
- MySQL问题记录——定义timestamp类型的数据
MySQL问题记录——定义timestamp类型的数据 摘要:本文主要记录了在使用MySQL的过程中定义timestamp类型数据时遇到的问题以及解决方案. 问题重现 在Windows环境下安装MyS ...