Description

Give an integer array,find the longest increasing continuous subsequence in this array.

An increasing continuous subsequence:

  • Can be from right to left or from left to right.
  • Indices of the integers in the subsequence should be continuous.

O(n) time and O(1) extra space.

Example

For [5, 4, 2, 1, 3], the LICS is [5, 4, 2, 1], return 4.

For [5, 1, 2, 3, 4], the LICS is [1, 2, 3, 4], return 4.

解题:记录连续增大或者连续减少的个数,返回最大值。代码如下:

public class Solution {
/**
* @param A: An array of Integer
* @return: an integer
*/
public int longestIncreasingContinuousSubsequence(int[] A) {
// write your code here
int i_count = 1;//上升的时候的个数
int d_count = 1;//下降时候的个数
int temp = 1;
//注意,当下标有减号时,要注意返回,下标不为负
if(A.length == 0){
return 0;
}
for(int i = 1; i < A.length; i++){
if(A[i] > A[i-1]){
//说明增大
temp++;
}else{
//否则
if(temp > i_count){
i_count = temp;//更新
}
temp = 1;
}
}
if(temp > i_count){
//如果一直到最后,可能缺少一次跟新
i_count = temp;
}
temp = 1;
for(int i = 1; i < A.length; i++){
if(A[i] < A[i-1]){
//说明减小
temp++;
}else{
//否则
if(temp > d_count){
d_count = temp;//更新
}
temp = 1;
}
}
if(temp > d_count){
//如果一直到最后,可能缺少一次跟新
d_count = temp;
}
if(i_count >= d_count)
return i_count;
else return d_count;
}
}

397. Longest Continuous Increasing Subsequence的更多相关文章

  1. LeetCode 674. Longest Continuous Increasing Subsequence (最长连续递增序列)

    Given an unsorted array of integers, find the length of longest continuous increasing subsequence. E ...

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

    Given an unsorted array of integers, find the length of longest continuous increasing subsequence. E ...

  3. [Swift]LeetCode674. 最长连续递增序列 | Longest Continuous Increasing Subsequence

    Given an unsorted array of integers, find the length of longest continuous increasing subsequence (s ...

  4. [Leetcode]674. Longest Continuous Increasing Subsequence

    Given an unsorted array of integers, find the length of longest continuous increasing subsequence. E ...

  5. [LeetCode&Python] Problem 674. Longest Continuous Increasing Subsequence

    Given an unsorted array of integers, find the length of longest continuousincreasing subsequence (su ...

  6. leetcode300. Longest Increasing Subsequence 最长递增子序列 、674. Longest Continuous Increasing Subsequence

    Longest Increasing Subsequence 最长递增子序列 子序列不是数组中连续的数. dp表达的意思是以i结尾的最长子序列,而不是前i个数字的最长子序列. 初始化是dp所有的都为1 ...

  7. 674. Longest Continuous Increasing Subsequence最长连续递增子数组

    [抄题]: Given an unsorted array of integers, find the length of longest continuous increasing subseque ...

  8. LeetCode Longest Continuous Increasing Subsequence

    原题链接在这里:https://leetcode.com/problems/longest-continuous-increasing-subsequence/description/ 题目: Giv ...

  9. 674. Longest Continuous Increasing Subsequence@python

    Given an unsorted array of integers, find the length of longest continuous increasing subsequence (s ...

随机推荐

  1. Jmeter--调度器配置

    Jmeter的线程组设置里有一个调配器设置,用于设置该线程组下脚本执行的开始时间.结束时间.持续时间及启动延迟时间.当需要半夜执行性能测试时会用到这个功能. ps:设置调度器配置,需要将前面的循环次数 ...

  2. Linux下的压缩解压缩命令

    *.tar 解包:tar xvf FileName.tar 打包:tar cvf FileName.tar DirName (注:tar是打包,不是压缩!) ——————————————— *.gz ...

  3. 基于uIP和uC/OS-II嵌入式网络开发

    基于uIP和uC/OS-II嵌入式网络开发 ——uIP主动发送数据分析 摘要:uIP协议栈简单易用,可以为16位单片机或者是更低级的处理器使用,占用的资源很少,相关移植网上有详细介绍,本文主要讨论uI ...

  4. Oracle数据库新装之后出现的监听程序无法正常启动和运行(Oracle-12514)

    修改安装目录下的配置文件      比如:F:\app\admin-PC\product\11.2.0\dbhome_1\network\admin\ 修改这个目录下的listener.ora和tns ...

  5. oracle表空间的创建+权限分配

    /*分为四步 */ /*第1步:创建临时表空间 */ create temporary tablespace user_temp tempfile 'D:\oracle\oradata\Oracle9 ...

  6. springboot(2.0以上) --数据源切换时报错

    在进行数据源切换时spring.datasource.type类型根据源码所给的默认值修改后依然报错 先看源码:标色部分 ,  就是springboot所给的数据源 ,  正常来说只要在配置文件中修改 ...

  7. 在tornado中使用异步mysql操作

    在使用tornado框架进行开发的过程中,发现tornado的mysql数据库操作并不是一步的,造成了所有用户行为的堵塞.tornado本身是一个异步的框架,要求所有的操作都应该是异步的,但是数据库这 ...

  8. HTML5新标签兼容——> <!--<if lt IE 9><!endif-->

    第一种方法:(使用html5shiv) <!--[if lt IE9]> <script src="http://html5shiv.googlecode.com/svn/ ...

  9. 爬取豆瓣电影Top250

    1 import json import requests from requests.exceptions import RequestException import re import time ...

  10. “==”与equals的区别

    “==”与equals的区别: “==”:两个对象比较的是对象的引用地址比较,对象的hashCode值是对象的引用地址,只有两个对象的hashCode值一样,此比较符才会返回true,否则即使两个对象 ...