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
  • 相关阅读:
    零散
    修改element的内部样式的两种方式
    在vue-cli项目中使用第三方的js,不是es6格式
    Docker知识
    golang使用grpc
    vue中axios导出文件
    nginx、vue和thinkphp配置
    Mysql的一些问题
    数据库索引失效原因
    golang中使用grpc服务
  • 原文地址:https://www.cnblogs.com/immjc/p/7503867.html
Copyright © 2011-2022 走看看