鏈接:https://leetcode.com/problems/longest-continuous-increasing-subsequence/description/
難度:Easy
題目:674. Longest Continuous Increasing Subsequence
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.
翻譯:給定一個(gè)無序整型數(shù)組宠漩,找出最長的連續(xù)增長的子序列妇汗。
思路:設(shè)置一個(gè)標(biāo)志符flag锉罐,遍歷整個(gè)數(shù)組辐烂,如果后一個(gè)值比前一個(gè)值大颓芭,則res取res和flag中的最大值辛块,否則flag置1郊霎。
參考代碼:
Java
class Solution {
public int findLengthOfLCIS(int[] nums) {
if(nums.length <=1)
return nums.length;
int res = 0;
int flag = 0;
for(int i=0; i<nums.length; i++)
if(i==0 || nums[i]>nums[i-1])
res = Math.max(res, ++flag);
else
flag = 1;
return res;
}
}