题目
UB0VM`_LIX}C9Y~9WCNKWFL.png
思路
遍历数组时,设置temp记录当前段的递增长度,设置ans记录数组的峰值情况,当遇到递增时加一,当遇到递减时更新temp和ans的字段。遍历结束后返回ans。
代码
class Solution {
public int findLengthOfLCIS(int[] nums) {
if(nums.length == 0){
return 0;
}
int count = 1,ans=1;
for(int i = 0; i<nums.length-1;i++){
if(nums[i]<nums[i+1]){
count++;
}else{
ans = ans>count?ans:count;
count = 1;
}
}
ans = ans>count?ans:count;
return ans;
}
}
- 注:将更新操作放在else中结果耗时更短