题目描述
给定无序整数数组,计算最长连续递增子序列的长度。
我的解法
- 遍历一遍
- beat 70%
class Solution { public int findLengthOfLCIS(int[] nums) { int len = nums.length; if(len == 0) return 0; int maxcon = 0; int nowcon = 1; for(int i = 1;i < len;i++){ if(nums[i - 1] < nums[i]) nowcon++; else{ if(maxcon < nowcon) maxcon = nowcon; nowcon = 1; } } if(maxcon < nowcon) maxcon = nowcon; return maxcon; } }
简便解法
public int findLengthOfLCIS(int[] nums) { int res = 0, cnt = 0; for(int i = 0; i < nums.length; i++){ if(i == 0 || nums[i-1] < nums[i]) res = Math.max(res, ++cnt); else cnt = 1; } return res; }