ts实现
function findLengthOfLCIS(nums: number[]): number {
if (nums.length === 1) return 1
let head: number = 0
let next: number = 1
let result: number = 1
while (next !== nums.length) {
nums[next - 1] < nums[next]
? (result = result <= next - head + 1 ? next - head + 1 : result)
: (head = next)
next++
}
return result
}
之前java代码实现
package com.wztlink1013.problems.leetcode.editor.cn;
// P674.最长连续递增序列
// P674.longest-continuous-increasing-subsequence
//给定一个未经排序的整数数组,找到最长且 连续递增的子序列,并返回该序列的长度。
//
// 连续递增的子序列 可以由两个下标 l 和 r(l < r)确定,如果对于每个 l <= i < r,都有 nums[i] < nums[i + 1] ,那
//么子序列 [nums[l], nums[l + 1], ..., nums[r - 1], nums[r]] 就是连续递增子序列。
//
//
//
// 示例 1:
//
//
//输入:nums = [1,3,5,4,7]
//输出:3
//解释:最长连续递增序列是 [1,3,5], 长度为3。
//尽管 [1,3,5,7] 也是升序的子序列, 但它不是连续的,因为 5 和 7 在原数组里被 4 隔开。
//
//
// 示例 2:
//
//
//输入:nums = [2,2,2,2,2]
//输出:1
//解释:最长连续递增序列是 [2], 长度为1。
//
//
//
//
// 提示:
//
//
// 0 <= nums.length <= 104
// -109 <= nums[i] <= 109
//
// Related Topics 数组
// 👍 147 👎 0
public class P674LongestContinuousIncreasingSubsequence{
public static void main(String[] args) {
Solution solution = new P674LongestContinuousIncreasingSubsequence().new Solution();
int [] nums = {1,3,5,7};
int result = solution.findLengthOfLCIS(nums);
System.out.println(result);
}
//leetcode submit region begin(Prohibit modification and deletion)
class Solution {
public int findLengthOfLCIS(int[] nums) {
if (nums.length == 0) { return 0; }
int result = 1;
int count = 1;
for (int i=0; i<nums.length-1; i++) {
if (nums[i] < nums[i+1] ) {
count++;
if (result < count) {result = count;}
} else {
if (result < count) {result = count;}
count = 1;
}
}
return result;
}
}
//leetcode submit region end(Prohibit modification and deletion)
}