[LeetCode]Longest Continuous Increasing Subsequence 最长连续增长序列

96
繁著
2017.12.08 00:10 字数 140

链接https://leetcode.com/problems/longest-continuous-increasing-subsequence/description/
难度:Easy
题目:674. Longest Continuous Increasing Subsequence
Given an unsorted array of integers, find the length of longest continuous increasing subsequence (subarray).
Example 1:

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. 

Example 2:

Input: [2,2,2,2,2]
Output: 1
Explanation: The longest continuous increasing subsequence is [2], its length is 1. 

Note:

  • Length of the array will not exceed 10,000.

翻译:给定一个无序整型数组,找出最长的连续增长的子序列。

思路:设置一个标志符flag,遍历整个数组,如果后一个值比前一个值大,则res取res和flag中的最大值,否则flag置1。

参考代码
Java

class Solution {
    public int findLengthOfLCIS(int[] nums) {
        if(nums.length <=1)
            return nums.length;
        int res = 0;
        int flag = 0;
        for(int i=0; i<nums.length; i++)
            if(i==0 || nums[i]>nums[i-1])
                res = Math.max(res, ++flag);
            else
                flag = 1;
        return res;
    }
}
校招面试
Web note ad 1