Leetcode 674 最长连续子序列

  JAVA:

    public final int findLengthOfLCIS(int[] nums) {
        if (nums.length < 2) return nums.length;
        int len = nums.length, left = 0, right = 1, re = 0;
        while (left < len && right < len) {
            int currRe = 1;
            while (nums[right] > nums[left]) {
                currRe++;
                left++;
                right++;
                if (right == len) return Math.max(currRe, re);
            }
            if (currRe > re) re = currRe;
            left = right;
            right++;
        }
        return re;
    }

  JS:

/**
 * @param {number[]} nums
 * @return {number}
 */
var findLengthOfLCIS = function (nums) {
    let len = nums.length;
    if (len < 2) return len;
    let left = 0, right = 1, re = 0;
    while (right < len) {
        let currRe = 1;
        while (right < len && nums[right] > nums[left]) {
            left++;
            right++;
            currRe++;
        }
        re = re > currRe ? re : currRe;
        left=right;
        right++;
    }
    return re;
};

 

posted @ 2021-03-03 21:54  牛有肉  阅读(49)  评论(0编辑  收藏  举报