LeetCode 674. 最长连续递增序列(Longest Continuous Increasing Subsequence) 18

674. 最长连续递增序列
674. Longest Continuous Increasing Subsequence

题目描述
给定一个未经排序的整型数组,找到最长且连续的递增序列。

Given an unsorted array of integers, find the length of longest continuous increasing subsequence (subarray).

每日一算法2019/5/21Day 18LeetCode674. Longest Continuous Increasing Subsequence

示例 1:

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

示例 2:

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

注意: 数组长度不会超过 10000。

Java 实现

class Solution {
    public int findLengthOfLCIS(int[] nums) {
        int res = 0, cnt = 0;
        for (int i = 0; i < nums.length; i++) {
            if (i == 0 || nums[i - 1] < nums[i]) {
                res = Math.max(res, ++cnt);
            } else {
                cnt = 1;
            }
        }
        return res;
    }
}

相似题目

参考资料

猜你喜欢

转载自www.cnblogs.com/hglibin/p/10899083.html