給定一個(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;
}
}