[LeetCode]Longest Continuous Increasing Subsequence 最长连续增长序列

链接https://leetcode.com/problems/longest-continuous-increasing-subsequence/description/
难度:Easy
题目: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.

翻译:给定一个无序整型数组,找出最长的连续增长的子序列。

思路:设置一个标志符flag,遍历整个数组,如果后一个值比前一个值大,则res取res和flag中的最大值,否则flag置1。

参考代码
Java

class Solution {
    public int findLengthOfLCIS(int[] nums) {
        if(nums.length <=1)
            return nums.length;
        int res = 0;
        int flag = 0;
        for(int i=0; i<nums.length; i++)
            if(i==0 || nums[i]>nums[i-1])
                res = Math.max(res, ++flag);
            else
                flag = 1;
        return res;
    }
}
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 背景 一年多以前我在知乎上答了有关LeetCode的问题, 分享了一些自己做题目的经验。 张土汪:刷leetcod...
    土汪阅读 12,769评论 0 33
  • 326. Power of Three Given an integer, write a function to...
    跑者小越阅读 2,157评论 0 1
  • 满怀期待,知道夏天载满遗憾。 人不是四季,会一载又一载的重来。 那么多失望堆积,把期盼推倒。 那么多故事里,千言万...
    木土有阿杜阅读 324评论 0 0
  • 思考:销售人员的心态调整:“其实人活的就是一种心态。心态调整好了,蹬着三轮车也可以哼小调;心态调整不好,开着宝马车...
    人生本是一场旅行阅读 269评论 0 0
  • 暗了,淡了,变了,走了,不见了。
    自言酱阅读 159评论 0 0