前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >leetcode-674-Longest Continuous Increasing Subsequence

leetcode-674-Longest Continuous Increasing Subsequence

作者头像
chenjx85
发布2018-05-22 16:36:54
5010
发布2018-05-22 16:36:54
举报

题目描述:

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。

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2018-05-11 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 题目描述:
  • 要完成的函数:
  • 说明:
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档