当前位置:网站首页>2022.05.24(LC_674_最长连续递增序列)

2022.05.24(LC_674_最长连续递增序列)

2022-06-10 18:16:00 Leeli9316

方法一:枚举

class Solution {
    public int findLengthOfLCIS(int[] nums) {
        int n = nums.length;
        int ans = 0;
        for (int i = 0; i < n; i++) {
            int pre = nums[i];
            int count = 1;
            for (int j = i + 1; j < n; j++) {
                if (nums[j] <= pre) break;
                count++;
                pre = nums[j];
            }
            ans = Math.max(ans, count);
        }
        return ans;
    }
}

 方法二:动态规划

class Solution {
    public int findLengthOfLCIS(int[] nums) {
        int n = nums.length;
        int[] dp = new int[n];
        Arrays.fill(dp, 1);
        int ans = 1;
        for (int i = 1; i < n; i++) {
            if (nums[i] > nums[i - 1]) {
                dp[i] = dp[i - 1] + 1;
            }
            ans = Math.max(ans, dp[i]);
        }
        return ans;
    }
}

 方法三:贪心

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

原网站

版权声明
本文为[Leeli9316]所创,转载请带上原文链接,感谢
https://blog.csdn.net/Leeli9316/article/details/124943263