博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
[LeetCode] 674. Longest Continuous Increasing Subsequence
阅读量:6291 次
发布时间:2019-06-22

本文共 1419 字,大约阅读时间需要 4 分钟。

Problem

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.

Solution #1 using index

class Solution {    public int findLengthOfLCIS(int[] nums) {        if (nums == null || nums.length == 0) return 0;        int start = 0, max = 1, pre = nums[0];        for (int i = 1; i < nums.length; i++) {            if (nums[i] > pre) {                pre = nums[i];                max = Math.max(max, i-start+1);            } else {                pre = nums[i];                start = i;            }        }        return max;    }}

Solution #2 using count

class Solution {    public int findLengthOfLCIS(int[] nums) {        if (nums == null || nums.length == 0) return 0;        int count = 1, max = 1;        for (int i = 1; i < nums.length; i++) {            if (nums[i] > nums[i-1]) {                count++;                max = Math.max(max, count);            } else {                count = 1;            }        }        return max;    }}

转载地址:http://frkta.baihongyu.com/

你可能感兴趣的文章
Wannafly挑战赛3
查看>>
解决Ubuntu14.04安装Chrome浏览器打不开的问题
查看>>
INSERT IGNORE 与INSERT INTO的区别
查看>>
JQuery Easy UI 简介
查看>>
【转帖】【面向代码】学习 Deep Learning(四) Stacked Auto-Encoders(SAE)
查看>>
垂直剧中
查看>>
C语言版推箱子
查看>>
BeanPostProcessor出现init方法无法被调用Invocation of init method failed
查看>>
WIN7 X64的运行命令窗口
查看>>
JS Promise API
查看>>
探究JS中的连等赋值问题
查看>>
113. Path Sum II
查看>>
小程序 显示Toobar
查看>>
react列表数据显示
查看>>
SpringMVC中与Spring相关的@注解
查看>>
在线求助 man page(转)
查看>>
Android基础控件SeekBar拖动条的使用
查看>>
PTA基础编程题目集6-2多项式求值(函数题)
查看>>
中国大学MOOC-JAVA学习(浙大翁恺)—— 信号报告
查看>>
Linux下Firefox汉化方法
查看>>