求一个无序数组的最长连续递增序列
发布时间
阅读量:
阅读量
题目描述
给定一个未经排序的整数数组,找到最长且连续的的递增序列。
示例 1:
输入: [1,3,5,4,7]
输出: 3
最大连续递增子序列是 [1, 3, 5] ,其长度为三
输入: [2,2,2,2,2]
输出: 1
解释: 最长连续递增序列是 [2], 长度为1。
注意:数组长度不会超过10000。
package leetcode;
/** * 给定一个未经排序的整数数组,找到最长且连续的的递增序列。
*/
class Solution {
public int findLengthOfLCIS(int[] nums) {
int numsLen = nums.length;
int max = 0;
int cur = 0;
// 若数组序列长度小于2(不包括2),直接输出长度
if (numsLen < 2) {
return numsLen;
}
for (int i=0; i<numsLen-1; i++) {
// 连续递增的数组元素
cur++;
if (max < cur) {
max = cur;
}
// 在不连续的数组元素的地方作为一个断点
if (nums[i] >= nums[i+1]) {
// 中断数组中连续的元素递增,重新开始
cur = 0;
}
}
cur++;
if (max < cur) {
max = cur;
}
return max;
}
}
public class Test{
public static void main(String[] args) {
int[] nums = {1,3,1,2,3,4};
Solution1 solution1 = new Solution1();
System.out.println(solution1.findLengthOfLCIS(nums));
}
}

通过LeetCode的测试用例:

全部评论 (0)
还没有任何评论哟~
