leetcode-674

72次阅读

共计 545 个字符,预计需要花费 2 分钟才能阅读完成。

public int findLengthOfLCIS(int[] nums) {if(nums == null || nums.length == 0)
            return 0;
            
        int sum = 1;
        int p = 0;
        int[] count = new int[nums.length];
        
        for(int i = 1; i < nums.length; i++){if(nums[i-1] < nums[i]){sum++;}else{count[p] = sum;
                sum = 1;
                p = i;
            }

        }
        
        count[p] = sum;
        int max = 0;
        for(int i = 0; i < count.length; i++){if(count[i] > max)
                max = count[i];
        }
        return max;
}

法二,用临时变量,减少了空间

public static int findLengthOfLCIS(int[] nums) {if(nums.length == 0 || nums == null)
            return 0;
        int sum = 1;
        int max = 0;

        for(int i = 1; i < nums.length; i++){if(nums[i-1] < nums[i]){sum++;}else{max = Math.max(max, sum);
                sum = 1;
            }

        }
        max = Math.max(max, sum);

        return max;
}

正文完
 0