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.
最长连续递增序列。我一开始以为是一个偏向滑动窗口的题但是后来我想多了,因为题目要求这个递增序列要是连续的所以会简单很多,直接看代码吧。
时间O(n)
空间O(1)
Java实现
1 class Solution { 2 public int findLengthOfLCIS(int[] nums) { 3 // corner case 4 if (nums == null || nums.length == 0) { 5 return 0; 6 } 7 8 // normal case 9 int res = 1; 10 int count = 1; 11 for (int i = 1; i < nums.length; i++) { 12 if (nums[i] > nums[i - 1]) { 13 count++; 14 res = Math.max(res, count); 15 } else { 16 count = 1; 17 } 18 } 19 return res; 20 } 21 }