首页 > 其他 > 详细

最长上升连续子序列 Linkcode

时间:2017-07-20 23:11:36      阅读:297      评论:0      收藏:0      [点我收藏+]

问题:

给定一个整数数组(下标从 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.

给定 [1, 1, 1, 1, 1], 其最长上升连续子序列(LICS)为 [1], 返回 1.

 1 public class Solution {
 2     /**
 3      * @param A an array of Integer
 4      * @return  an integer
 5      */
 6     public static int longestIncreasingContinuousSubsequence(int[] A) {
 7         if(A.length==0)
 8             return 0;
 9         int hz,hj,r,maxz,maxj;
10         hz=hj=0;
11         r=maxz=maxj=1;
12         
13         while(r<A.length){
14             if(A[r]<A[r-1]){
15                 maxz = Math.max(maxz, r-hz);
16                 hz = r;
17             }else if(A[r]>A[r-1]){
18                 maxj = Math.max(maxj, r-hj);
19                 hj = r;
20             }else{
21                 hz = hj = r;
22             }
23             r++;
24         } 
25         maxz = Math.max(maxz, r-hz);
26         maxj = Math.max(maxj, r-hj);
27         return Math.max(maxz, maxj);
28     }
29 }

 

最长上升连续子序列 Linkcode

原文:http://www.cnblogs.com/xfcnbkk/p/7215049.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!