zoukankan      html  css  js  c++  java
  • leetcode-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.

     

    要完成的函数:

    int findLengthOfLCIS(vector<int>& nums) 

     

    说明:

    1、这道题目给了一个vector,要求找到里面最长的连续升序子vector,返回它的长度,十分容易的一道题。

    2、笔者的想法是,从第一位开始找起,迭代下去,直到下一个元素小于等于当前元素,把长度记录下来。

    接着从下一个元素开始找起,直到又不满足升序的条件,然后记录长度,与上次得到的长度比较,保留大的数值。

    一直处理,直到vector的最后一位。

    代码如下:

        int findLengthOfLCIS(vector<int>& nums) 
        {
            int length=1,i=0,s1=nums.size(),max1=1;
            if(s1==0)
                return 0;
            while(i<s1-1)
            {
                if(nums[i]<nums[i+1])
                {
                    length++;
                    i++;
                }
                else
                {
                    max1=max(max1,length);//记录大的长度
                    i++;//i更新到下一个元素
                    length=1;//重置length
                }
            }
            return max(max1,length);//当碰到类似于[1,3,5,7,9]的测试样例时
        }

    上述代码实测16ms,beats 69.47% of cpp submissions。

  • 相关阅读:
    Core Java Interview Question Answer
    Anagrams
    Permutations II
    Minimum Window Substring
    工厂模式
    How do you design object oriented projects?
    What is Object Oriented Design? (OOD)
    Amazon Interview Question: Design an OO parking lot
    讨论一道求质数的面试题
    Substring with Concatenation of All Words
  • 原文地址:https://www.cnblogs.com/chenjx85/p/9024445.html
Copyright © 2011-2022 走看看