zoukankan      html  css  js  c++  java
  • [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
  • 相关阅读:
    设置装订线
    设置页边距
    查看压缩文件的信息
    格式化3
    格式化2
    条件格式化1
    单独设置奇偶页的页眉页脚
    方法重写注意点
    super注意点
    稀疏数组的压缩和还原
  • 原文地址:https://www.cnblogs.com/immjc/p/7503867.html
Copyright © 2011-2022 走看看