LeetCode674-最长连续递增序列

题目链接

英文链接:https://leetcode.com/problems/longest-continuous-increasing-subsequence/

中文链接:https://leetcode-cn.com/problems/longest-continuous-increasing-subsequence/

题目详述

给定一个未经排序的整数数组,找到最长且连续的的递增序列。

示例 1:

1
2
3
4
输入: [1,3,5,4,7]
输出: 3
解释: 最长连续递增序列是 [1,3,5], 长度为3。
尽管 [1,3,5,7] 也是升序的子序列, 但它不是连续的,因为5和7在原数组里被4隔开。

示例 2:

1
2
3
输入: [2,2,2,2,2]
输出: 1
解释: 最长连续递增序列是 [2], 长度为1。

注意:数组长度不会超过10000。

题目详解

LeetCode300-最长上升子序列 序列不要求连续,本题要求连续,比较简单。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
public class LeetCode_00674 {

public int findLengthOfLCIS(int[] nums) {
if (nums == null || nums.length == 0) {
return 0;
}
int cnt = 1;
int res = 1;
for (int i = 1; i < nums.length; ++i) {
if (nums[i] > nums[i - 1]) {
res = Math.max(res, ++cnt);
} else {
cnt = 1;
}
}
return res;
}
}