LintCode刷题——最长上升连续子序列
2017-09-23 22:08
274 查看
问题描述:
给定一个整数数组(下标从 0 到 n-1, n 表示整个数组的规模),请找出该数组中的最长上升连续子序列。(最长上升连续子序列可以定义为从右到左或从左到右的序列。)
样例:
给定
其最长上升连续子序列(LICS)为
给定
其最长上升连续子序列(LICS)为
解决:
给定一个整数数组(下标从 0 到 n-1, n 表示整个数组的规模),请找出该数组中的最长上升连续子序列。(最长上升连续子序列可以定义为从右到左或从左到右的序列。)
样例:
给定
[5, 4, 2, 1, 3],
其最长上升连续子序列(LICS)为
[5, 4, 2, 1], 返回
4.
给定
[5, 1, 2, 3, 4],
其最长上升连续子序列(LICS)为
[1, 2, 3, 4], 返回
4.
解决:
public class Solution { /* * @param : An array of Integer * @return: an integer */ public int longestIncreasingContinuousSubsequence(int[] array) { if(array.length==0){ return 0; } int increasingResult=1; int unicreasingResult=1; int currentIncreasingResult=1; int currentUnicreasingResult=1; for(int i=1;i<array.length;i++) { //最长连续递增 if(array[i]>array[i-1]) { currentIncreasingResult++; }else { currentIncreasingResult=1; } increasingResult=Integer.max(increasingResult, currentIncreasingResult); //最长连续递减 if(array[i]<array[i-1]) { currentUnicreasingResult++; }else { currentUnicreasingResult=1; } unicreasingResult=Integer.max(unicreasingResult, currentUnicreasingResult); } return unicreasingResult>increasingResult?unicreasingResult:increasingResult; } }
相关文章推荐
- lintcode-最长上升连续子序列-397
- lintcode 最长上升连续子序列 II(二维最长上升连续序列)
- lintcode-397-最长上升连续子序列
- 最长上升连续子序列-LintCode
- LintCode- 最长上升连续子序列
- LintCode 最长上升连续子序列
- LintCode:最长上升连续子序列
- lintcode:最长上升连续子序列
- 最长连续序列-LintCode
- python_lintcode_397最长上升连续子序列_56两数之和
- *[Lintcode]Longest Consecutive Sequence最长连续序列
- 最长上升子序列-LintCode
- [LintCode] 619 Binary Tree Longest Consecutive Sequence III 二叉树最长连续序列 III
- Lintcode 114.最大上升连续子序列
- LintCode(easy)最长上升连续子序列
- lintcode ----最长连续序列
- 题目:最长上升连续子序列
- 最长上升连续子序列
- 【LintCode】最长上升序列(Longest Increasing Sequence)
- 最长上升连续子序列