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;
}
}

最新文章

  1. 高性能 TCP &amp; UDP 通信框架 HP-Socket v3.3.1
  2. JsonHelper MergeJsonTemplate
  3. IntelliJ IDEA 编译maven项目以及运行测试前编译项目
  4. android-android获取navigationview 上的控件id
  5. mysql数据库的主从
  6. 【C++】error C4146: 一元负运算符应用于无符号类型,结果仍为无符号类型
  7. 高效的iOS宏定义
  8. 安装Nuget上常用的包的命令
  9. Linux Chaining Operators用法学习
  10. PHP手册应注意
  11. easyui form提交文件(上传图片和文件)
  12. c#实现list,dataset,DataTable转换成josn等各种转换方法总和
  13. JAVA读取和写入properties文件
  14. Asp.Net Core 2.0 之旅---在Ubuntu上部署WEB应用程序
  15. ELK+filebeat、kafka、zookeeper搭建文档
  16. 干货满满,腾讯云+社区技术沙龙 Kafka Meetup 深圳站圆满结束
  17. LODOP打印当前日期时间的方法
  18. Ubuntu下安装git
  19. helm详解
  20. Spring XML配置里的Bean自动装配

热门文章

  1. 如何安装zip格式的MySQL
  2. OpenCV 中CV_IMAGE_ELEM 的使用
  3. 让NSArray数组中每个对象都调用的方法
  4. MyEclipse 根据左括号或右括号查找另外一半
  5. HTML&amp;CSS笔记001
  6. Redis笔记 -- 在 Centos7.4单机中部署Redis集群(二)
  7. Swift_TableView(delegate,dataSource,prefetchDataSource 详解)
  8. 复习宝典之Maven项目管理
  9. 远程连接Oracle 服务器 解决Oracle查询中文乱码
  10. DOCTYPE导致MyEclipse无法正常格式化HTML的问题