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;
}
}