397. Longest Continuous Increasing Subsequence
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的更多相关文章
- LeetCode 674. Longest Continuous Increasing Subsequence (最长连续递增序列)
Given an unsorted array of integers, find the length of longest continuous increasing subsequence. E ...
- [LeetCode] Longest Continuous Increasing Subsequence 最长连续递增序列
Given an unsorted array of integers, find the length of longest continuous increasing subsequence. E ...
- [Swift]LeetCode674. 最长连续递增序列 | Longest Continuous Increasing Subsequence
Given an unsorted array of integers, find the length of longest continuous increasing subsequence (s ...
- [Leetcode]674. Longest Continuous Increasing Subsequence
Given an unsorted array of integers, find the length of longest continuous increasing subsequence. E ...
- [LeetCode&Python] Problem 674. Longest Continuous Increasing Subsequence
Given an unsorted array of integers, find the length of longest continuousincreasing subsequence (su ...
- leetcode300. Longest Increasing Subsequence 最长递增子序列 、674. Longest Continuous Increasing Subsequence
Longest Increasing Subsequence 最长递增子序列 子序列不是数组中连续的数. dp表达的意思是以i结尾的最长子序列,而不是前i个数字的最长子序列. 初始化是dp所有的都为1 ...
- 674. Longest Continuous Increasing Subsequence最长连续递增子数组
[抄题]: Given an unsorted array of integers, find the length of longest continuous increasing subseque ...
- LeetCode Longest Continuous Increasing Subsequence
原题链接在这里:https://leetcode.com/problems/longest-continuous-increasing-subsequence/description/ 题目: Giv ...
- 674. Longest Continuous Increasing Subsequence@python
Given an unsorted array of integers, find the length of longest continuous increasing subsequence (s ...
随机推荐
- 【题解】洛谷P2023 [AHOI2009] 维护序列(线段树)
洛谷P2023:https://www.luogu.org/problemnew/show/P2023 思路 需要2个Lazy-Tag 一个表示加的 一个表示乘的 需要先计算乘法 再计算加法 来自你谷 ...
- CSU - 1224 ACM小组的古怪象棋
传送门: http://acm.csu.edu.cn/csuoj/problemset/problem?pid=1224 1224: ACM小组的古怪象棋 Lime Limit: 1 Sec ...
- Vue教程:指令与事件(二)
一.插值 v-once 通过使用 v-once 指令,你也能执行一次性地插值,当数据改变时,插值处的内容不会更新.但请留心这会影响到该节点上所有的数据绑定: span v-once>这个将不会改 ...
- Reading Notes : 180214 计算机的总线结构
读书<计算机组成原理>,百度百科 基本上接触过计算机的人,都多少知道计算机的具体构成,但是真正能讲明白的却说了很多,本节将讲解一下计算机的基本硬件构成和一些基本信息,简单认识,以后再深入了 ...
- Head First Java学习笔记
1.基本概念 1.1.工作方式 源代码(.java)---编译器(执行javac程序)---产生字节码(.class与平台无关)---JAVA虚拟机(JVM,读取与执行字节码) 1.2.汇编语言是对基 ...
- 算法基础-dfs
最近在学dfs(深度优先搜索),dfs与树的遍历差不多,就是先从一个点开始一直搜索,直到走不动为止.现在推荐一个简单的dfs题, 百炼的2815, ########################## ...
- laravel 5.7 resources 本地化 简体中文
使用方法: 新建目录[项目目录/resources/lang/zh] 按以下内容创建文件,并将内容复制到文件中 修改 config/app.php 'locale' => 'zh', 'fall ...
- Dubbo 安装ZooKeeper环境
一.在Windows 安装ZooKeeper 1.下载ZooKeeper 2.解压,修改ZooKeeper配置文件 复制一份zoo_sample.cfg文件,改名位zoo.cfg,打开编辑,设置数据保 ...
- Eclipse切换字体颜色
打开window-preferences
- 新人成长之入门Vue.js弹窗Dialog介绍(二)
前言 在上一篇博文中介绍了Vue.js的常用指令,今天总结归纳一下弹窗Dialog的使用,弹窗经常被使用在一些表单的增删改查啊,或者弹出一些提示信息等等,在保留当前页面状态的情况下,告知用户并承载相关 ...