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: ...
随机推荐
- Codeforces Global Round 5
传送门 A. Balanced Rating Changes 签到,分正负搞一下就行. B. Balanced Tunnel 题意: 给出\(n\)辆车的进洞顺序和出洞顺序,问有多少量车实现了洞中超车 ...
- 基于UDP协议的socket套接字编程
目录 一.UDP套接字简单示例 1.1 服务端 二.客户端 三.UPD套接字无粘包问题 3.1 服务端 3.2 客户端 四.qq聊天 4.1 服务端 4.2 客户端1 4.3 客户端2 4.4 运行结 ...
- Protractor - 环境设置
去年出于好奇搭建过一个Protractor+Cucumber的测试框架,当时项目上并没有用到AngularJS,所以框架能运行起来之后没有再深入了.最近新项目引入了AngularJS,想起去年搭的那个 ...
- Docker的入门及常用命令
Docker入门及常用命令 1. 各个容器之间是相互隔离状态: 这样减少了我们软件之间的影响. 2. docker是os层虚拟化架构的一种产品体现, os层虚拟化架构出来的操作系统需要和宿主机操作系统 ...
- Fusionstorage的逻辑架构
Fusionstorage Fusionstorage的逻辑架构 Mdc:元数据控制,实现对分布式集群的状态控制,以及控制数据分布式规则,数据重建规则等,mdc默认部署在3个节点的zk盘上,形成mdc ...
- MySQL中的group_concat函数的使用
本文通过实例介绍了MySQL中的group_concat函数的使用方法,比如select group_concat(name) . MySQL中group_concat函数 完整的语法如下: grou ...
- LabVIEW工控二进制数据存储
在文件存储的逻辑上,二进制文件基于值编码,而不是字符编码,其占用空间小,读取/写入速度快,但是译码比较复杂,不利用数据共享.根据具体编码方式的不同,二进制的使用方式也有所不同,如对bmp格式,规定了文 ...
- C#委托内部使用局部的变量的问题
一. 引子 先来看如下代码: ; Action action1 = () => { Console.WriteLine("打印一下i的值:" + i); }; i = ; A ...
- element-admin中echarts图标宽度无法修改
默认示例 <template> <div> <el-row :gutter="0"> <el-col :xs="24" ...
- grep命令提示"binary file matches **.log"解决方法
仔细想想,这个问题遇到很多次了,之前一直以为很复杂,一搜索发现解决这么简单,记录一下做备忘. grep test XXX.log Binary file app.log matches 此时使用-a参 ...