leedcode py 0724
时间: 2023-07-29 16:11:39 浏览: 39
题目描述:
给定一个整数数组,找到并返回其中最长的连续上升子序列的长度。
示例 1:
输入: [1,3,5,4,7]
输出: 3
解释: 最长的连续上升子序列是 [1,3,5], 长度为3。
尽管 [1,3,4,7] 也是升序的子序列, 但它不是连续的,因为 4 在序列中被视为一个单独的数字。
示例 2:
输入: [2,2,2,2,2]
输出: 1
解释: 最长的连续上升子序列是 [2], 长度为1。
提示:
输入的数组长度范围是 [1, 10^4]。
输入的数组中可能包含重复元素,所以需要去重。
Python 代码实现:
```python
class Solution:
def findLengthOfLCIS(self, nums: List[int]) -> int:
if not nums:
return 0
n = len(nums)
ans = 1
cur_len = 1
for i in range(1, n):
if nums[i] > nums[i-1]:
cur_len += 1
else:
ans = max(ans, cur_len)
cur_len = 1
ans = max(ans, cur_len)
return ans
```
参考链接:https://leetcode-cn.com/problems/longest-continuous-increasing-subsequence/