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
  • 相关阅读:
    订阅发布模式概念基础
    HTML与CSS之display:none
    HTML与CSS之导航栏
    微信小程序-笔记
    基于设计模式的用户管理
    微信小程序-开发组件-笔记1
    JavaScript异常处理
    微信小程序开发-笔记
    封装类实现增删改查
    脚本方式实现数据库增删改查
  • 原文地址:https://www.cnblogs.com/immjc/p/7503867.html
Copyright © 2011-2022 走看看