自学内容网 自学内容网

连续递增最长子序列--中等

题目

给定一个未经排序的整数数组,找到最长且 连续递增的子序列,并返回该序列的长度。

  • 示例 1:
    输入:nums = [1,3,5,4,7]
    输出:3
    解释:最长连续递增序列是 [1,3,5], 长度为3。
    尽管 [1,3,5,7] 也是升序的子序列, 但它不是连续的,因为 5 和 7 在原数组里被 4 隔开。

  • 示例 2:
    输入:nums = [2,2,2,2,2]
    输出:1
    解释:最长连续递增序列是 [2], 长度为1。

代码

public class FindLengthOfLCIS {
    
    public static int findLengthOfLCIS(int[] nums) {
        int result = 1;
        int count = 1;
        for (int i = 0; i < nums.length-1; i++) {
            if (nums[i + 1] > nums[i]) {
                count++;
            } else {
                count = 1;
            }
            result = Math.max(result, count);
        }

        return result;
    }

    public static void main(String[] args) {
        int lengthOfLCIS = findLengthOfLCIS(new int[]{3, 5, 1, 2, 4, 6, 7, 5,6,7});
        System.out.println(lengthOfLCIS); // 5
    }
    
}

原文地址:https://blog.csdn.net/ZHUSHANGLIN/article/details/140521073

免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!