[LeetCode] Longest Continuous Increasing Subsequence

Given an unsorted array of integers, find the length of longest continuous increasing subsequence.

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.

求一个数列的最长连续递增子序列。使用两个变量记录数据,res记录当前最长的连续递增子序列长度,tmp记录当前的连续递增子序列长度。如果不满足连续递增,则令tmp为1,重新记录当前连续递增子序列长度。

class Solution {
public:
    int findLengthOfLCIS(vector<int>& nums) {
        int res = 1, tmp = 1;
        if (nums.empty())
            return 0;
        for (int i = 0; i != nums.size() - 1; i++) {
            if (nums[i] < nums[i + 1]) {
                tmp++;
                res =max(res, tmp);
            } 
            else {
                tmp = 1;
            }
        
        }
        return res;
    }
};
// 16 ms

 

posted @ 2017-09-11 10:45  immjc  阅读(147)  评论(0编辑  收藏  举报