LeetCode 674. Longest Continuous Increasing Subsequence最长连续递增序列 (C++/Java)
题目:
Given an unsorted array of integers, find the length of longest continuous increasing subsequence (subarray).
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.
分析:
给定一个未经排序的整数数组,找到最长且连续的的递增序列。
注意是要找到连续的递增序列,由于需要连续,这道题就变得简单了许多。我们可以创建一个和给定数组同样大小的数组res,用来记录到当前元素递增序列的长度。
| nums | 1 | 3 | 5 | 4 | 7 |
| res | 1 | 2 | 3 | 1 | 2 |
初始化res[0]为1,因为有一个数的话,大小也是1。如果当前元素大于前一个元素,则长度加1,否则,意味着当前元素无法和前面的序列继续构成递增这一条件,我们要计算后面的递增序列的大小,所以重新置为1。遍历完数组后,直接返回res中最大值即可。
当然我们也可以不适用数组来存储,可以发现比较数组元素是否递增时,如果保持递增,序列长度加1,那么我们可以创建两个变量,一个用来保存当前的递增序列长度,如果下一个元素符合条件,就加1,否则就重新置为1,另一个变量用来保存最终解,每一次更新当前递增序列长度,都和最终解比较大小,将大的值赋给最终解。
| nums | 1 | 3 | 5 | 4 | 7 |
| temp | 1 | 2 | 3 | 1 | 2 |
| res | 1 | 2 | 3 | 3 | 3 |
程序:
C++
class Solution {
public:
int findLengthOfLCIS(vector<int>& nums) {
if(nums.size() == ) return ;
int res = ;
int max_temp = ;
for(int i = ; i < nums.size(); ++i){
if(nums[i] > nums[i-])
++max_temp;
else
max_temp = ;
res = max(res, max_temp);
}
return res;
}
};
Java
class Solution {
public int findLengthOfLCIS(int[] nums) {
if(nums.length == 0) return 0;
int res = 1;
int maxTemp = 1;
for(int i = 1; i < nums.length; ++i){
if(nums[i-1] < nums[i])
++maxTemp;
else
maxTemp = 1;
res = Math.max(res, maxTemp);
}
return res;
}
}
LeetCode 674. Longest Continuous Increasing Subsequence最长连续递增序列 (C++/Java)的更多相关文章
- [LeetCode] 674. Longest Continuous Increasing Subsequence 最长连续递增序列
Given an unsorted array of integers, find the length of longest continuous increasing subsequence. E ...
- [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: ...
随机推荐
- 异常类的构建——顶层父类Exception的实现
异常类构建异常的类型可以是自定义类类型对于类类型异常的匹配依旧是从上到下严格的匹配赋值兼容性原则在异常匹配中依然适用一般而言-匹配子类异常的catch放在上部-匹配父类异常的catch放在下部 现代C ...
- vue项目搭建及创建、目录结构、项目启动、全局配置
Vue项目环境搭建 """ node ~~ python:node是用c++编写用来运行js代码的 npm(cnpm) ~~ pip:npm是一个终端应用商城,可以换国内 ...
- python文件操作【目录大全】
总是记不住API.昨晚写的时候用到了这些,但是没记住,于是就索性整理一下吧: python中对文件.文件夹(文件操作函数)的操作需要涉及到os模块和shutil模块. 得到当前工作目录,即当前Pyth ...
- spring cloud 2.x版本 Gateway熔断、限流教程
前言 本文采用Spring cloud本文为2.1.8RELEASE,version=Greenwich.SR3 本文基于前两篇文章eureka-server.eureka-client.eureka ...
- C# Event在.Net规则下由接口定义的实现
最近在学C#(教材<C# in a nutshell>很不错的说),看完delegate(委托)以后,紧接着就是event(事件)了,个人对跟.Net相关的东西并没有什么兴趣(毕竟是会增加 ...
- hibernate中的merge()方法
Hibernate提供有save().persist().savaOrUpdate()和merge()等方法来提供插入数据的功能.前三者理解起来较后者容易一些,而merge()方法从api中的介绍就可 ...
- python基础(6):基本数据类型一(int、bool、str)
1. 基本数据类型 1. int ==> 整数,主要⽤来进⾏数学运算 2. str ==> 字符串,可以保存少量数据并进⾏相应的操作 3. bool==>判断真假,True,Fals ...
- crm-全总结
1.什么是crm 客户关系管理系统 ,以客户数据为中心建立一个信息收集.管理.分析和利用的信息系统 2.业务逻辑相关使用crm-app完成 1)路由项目分发到crm-app (别名 传参 命名空间) ...
- 重新安装和更新所有的 nuget包
重新安装指定项目中所有的 nuget 包 Update-Package -ProjectName MyProject –reinstall 更新指定项目中所有的 nuget 包 Update-Pack ...
- nginx 配置不当导致目录遍历下载漏洞
今天做百度杯的时候发现一个题很有意思. 点进题目,发现了一个js重定向到login.php,抓包发现请求的header中cookie=0,做过这种类似的题目,o==false,在请求头里面将cooki ...