LintCode最長上升連續(xù)子序列

給定一個(gè)整數(shù)數(shù)組(下標(biāo)從 0 到 n-1, n 表示整個(gè)數(shù)組的規(guī)模),請(qǐng)找出該數(shù)組中的最長上升連續(xù)子序列。(最長上升連續(xù)子序列可以定義為從右到左或從左到右的序列。)

代碼如下:

public class Solution {
    /**
     * @param A an array of Integer
     * @return  an integer
     */
   public int longestIncreasingContinuousSubsequence(int[] nums) {
        if(null == nums || nums.length <= 0)
            return 0;
        int ascendCount = 1;
        int maxAscendCount = -1;
        for(int i = 1;i < nums.length;i++)
        {
            if(nums[i] > nums[i - 1])
            {
                ascendCount++;
                if(ascendCount > maxAscendCount)
                {
                    maxAscendCount = ascendCount;
                }
            }
            else
            {
                ascendCount = 1;
            }
        }
        
        if(ascendCount > maxAscendCount)
        {
            maxAscendCount = ascendCount;
        }
        ascendCount = 1;
        for(int i = nums.length - 1;i >= 1;i--)
        {
            if(nums[i] < nums[i - 1])
            {
                ascendCount++;
                if(ascendCount > maxAscendCount)
                {
                    maxAscendCount = ascendCount;
                }
            }
            else
            {
                ascendCount = 1;
            }
        }
        return maxAscendCount;
    }
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 題目 給定一個(gè)整數(shù)數(shù)組(下標(biāo)從 0 到 n-1, n 表示整個(gè)數(shù)組的規(guī)模),請(qǐng)找出該數(shù)組中的最長上升連續(xù)子序列。(...
    六尺帳篷閱讀 553評(píng)論 0 1
  • 給定一個(gè)整數(shù)數(shù)組(下標(biāo)從 0 到 n-1, n 表示整個(gè)數(shù)組的規(guī)模),請(qǐng)找出該數(shù)組中的最長上升連續(xù)子序列。(最長上...
    DayDayUpppppp閱讀 287評(píng)論 0 0
  • 版權(quán)聲明:本文為博主原創(chuàng)文章,未經(jīng)博主允許不得轉(zhuǎn)載。 難度:容易 要求: 給定一個(gè)整數(shù)數(shù)組(下標(biāo)從 0 到 n-1...
    柒黍閱讀 415評(píng)論 0 0
  • 問題: 給定一個(gè)整數(shù)數(shù)組(下標(biāo)從 0 到 n-1, n 表示整個(gè)數(shù)組的規(guī)模),請(qǐng)找出該數(shù)組中的最長上升連續(xù)子序列。...
    留十夜閱讀 879評(píng)論 0 0
  • 一、LintCode鏈接 最長上升連續(xù)子序列 二、問題描述 給定一個(gè)整數(shù)數(shù)組(下標(biāo)從 0 到 n-1, n 表示整...
    Summer舒舒閱讀 500評(píng)論 0 0

友情鏈接更多精彩內(nèi)容