当前位置:网站首页>Leetcode- longest continuous increasing sequence - simple

Leetcode- longest continuous increasing sequence - simple

2022-06-13 05:50:00 AnWenRen

title :674 The longest continuous increasing sequence - Simple

subject

Given an unordered array of integers , Find the longest and Successive increasing subsequences , And return the length of the sequence .

Successive increasing subsequences It can be made up of two subscripts l and r(l < r) determine , If for each l <= i < r, There are nums[i] < nums[i + 1] , So the subsequence [nums[l], nums[l + 1], …, nums[r - 1], nums[r]] It's a continuous increasing subsequence .

Example 1

 Input :nums = [1,3,5,4,7]
 Output :3
 explain : The longest continuous increasing sequence is  [1,3,5],  The length is 3.
 Even though  [1,3,5,7]  It's also a subsequence of ascending order ,  But it's not continuous , because  5  and  7  In the original array is  4  separate .

Example 2

 Input :nums = [2,2,2,2,2]
 Output :1
 explain : The longest continuous increasing sequence is  [2],  The length is 1.

Tips

  • 1 <= nums.length <= 104
  • -109 <= nums[i] <= 109

Code Java

public int findLengthOfLCIS(int[] nums) {
    
    int count = 1;
    int ans = 1;
    for (int i = 1; i < nums.length; i++) {
    
        if (nums[i] > nums[i - 1]) {
    
            count ++;
        } else {
    
            count = 1;
        }
        if (count > ans) {
    
            ans = count;
        }
    }
    return ans;
}
原网站

版权声明
本文为[AnWenRen]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202280507174844.html