给定一个未经排序的整数数组,找到最长且 连续递增的子序列,并返回该序列的长度。
连续递增的子序列 可以由两个下标 l
和 r
(l < r
)确定,如果对于每个 l <= i < r
,都有 nums[i] < nums[i + 1]
,那么子序列 [nums[l], nums[l + 1], ..., nums[r - 1], nums[r]]
就是连续递增子序列。
链接:674. 最长连续递增序列 - 力扣(LeetCode)
方法一:
class Solution {
public:
int findLengthOfLCIS(vector<int>& nums) {
int n = nums.size();
int j = 1;
int i;
int max = 1,current = 1;
for(i=0;i<n-1;i++){
if(nums[i]<nums[i+1]){
current++;
}else{
max = std::max(current,max);
current = 1;
}
}
max = std::max(current,max);//最后一次比较
return max;
}
};
此题比较简单,主要是不要忘记可能存在很多递增子数组,需要拿到后进行比较大小。其次就是不要忘记最后一次比较,当数组遍历完后是不经过else的,所以这次循环的current的数据还没有与max进行比较,所以需要最后比较一次得出最长递增数组。