674. Longest Continuous Increasi

2017-09-11  本文已影响0人  yxwithu

题目

Given an unsorted array of integers, find the length of longest continuous increasing subsequence.
Example:
Input: [1,3,5,4,7]
Output: 3
Explanation: The longest continuous increasing subsequence is [1,3,5], its length is 3.
Even though [1,3,5,7] is also an increasing subsequence, it's not a continuous one where 5 and 7 are separated by 4.

分析

给定一个数组,输出最大的连续递增序列长度。
两个指针,start和end,如果end +1 大于 end 则end ++,否则start和end重新开始,都置为end+1

代码

public int findLengthOfLCIS(int[] nums) {
    if(nums.length <= 1) return nums.length;
    int start = 0, end = 0, res = 0;
    while(end < nums.length - 1){
        if(nums[end+1] > nums[end]){
            end ++;
        }else{
            res = Math.max(res, end - start + 1);
            end ++;
            start = end;
        }
    }
    return Math.max(res, end - start + 1);
}
上一篇下一篇

猜你喜欢

热点阅读