原题链接:https://leetcode-cn.com/problems/longest-continuous-increasing-subsequence/
同样还是双指针的应用,思路和 【209. 长度最小的子数组】很像
设置maxCount为最大的连续长度maxCount=1(一个数字也算连续)
设置count为当前的连续长度,如果count>maxCount 则更新maxCount=count
开始吧,从第一个数字开始
此时count=1,不满足count>maxCount,则接着判断后者是否比当前大 3>1 则扩起
此时count=2,满足count>maxCount,则更新maxCount=2,接着判断后者是否比当前大 5>3 扩起
此时count=3,满足count>maxCount,则更新maxCount=3,接着判断后者是否比当前大 4<5 则从后者重新统计
此时count=1,不满足count>maxCount,接着判断后者是否比当前大 7>4 扩起
此时count=2,不满足count>maxCount,接着判断后者是否比当前大 只不过此时后者没元素,操作完毕
综上所述,maxCount=3
代码:
class Solution {
public int findLengthOfLCIS(int[] nums) {
if(nums.length==0||nums.length==1){
return nums.length;
}
int maxCount=1;
int count=1;
for(int i=0;i<nums.length-1;i++){
if(nums[i+1]>nums[i]){
count++;
}else{
count=1;
}
maxCount=count>maxCount?count:maxCount;
}
return maxCount;
}
}