类别:数组
题目:https://leetcode-cn.com/problems/que-shi-de-shu-zi-lcof/
我的解:
class Solution {
public int findLengthOfLCIS(int[] nums) {
int after = 0, last = 0, result = 1;
if(nums.length == 0){
return 0;
}
for (int i = 0; i < nums.length; ++i) {
if (i > 0 && nums[i-1] >= nums[i]) {
last = i;
//致命误区
result = last;
}
if (i > 0 && nums[i-1] < nums[i]){
after = i - last + 1;
}
if( after > result ){
result = after;
}
}
return result;
}
}
最优解:
class Solution {
public int findLengthOfLCIS(int[] nums) {
int ans = 0, anchor = 0;
for (int i = 0; i < nums.length; ++i) {
if (i > 0 && nums[i-1] >= nums[i]) anchor = i;
ans = Math.max(ans, i - anchor + 1);
}
return ans;
}
}
差异点:
1.判断方式基本一样;但是在上次长度时和当前长度做比较时,没想当可以用
i - last+ 1做上次的长度;而是last单独拎个一个第一次长度变量标识;

所以纠结这个特例时间比较耗时较久;
网友评论