# !/usr/bin/env python3
# -*- coding: UTF-8 -*-
"""
@author : v_jiaohaicheng@baidu.com
@des : 给定一个未经排序的整数数组,找到最长且 连续递增的子序列,并返回该序列的长度。
"""
class Solution:
def findLengthOfLCIS(self, nums) -> int:
# n2 第二个指针
n2 = 1
# 存所有递增子序列长度结果
res = []
# 临时存储符合条件的子序列
temp = []
# 当nums长度为1时直接返回1
if len(nums) == 1:
return 1
# 当第二个指针小于等于nums长度时(不越界)
while n2 <= len(nums)-1:
# 给第一个指针赋值
n1 = n2 - 1
# 判断两个指针对应的值是否满足 后大于前
if nums[n1]<nums[n2]:
# 初次进入,temp 为空时,直接将两个指针的值都存储到temp中
if len(temp) == 0:
temp = [nums[n1],nums[n2]]
# 非第一次进入,将第二个指针对应的值追加到temp中
else:
temp.append(nums[n2])
# 前大于后
else:
# 将结果写入res,并初始化temp为当前第二个指针所指示的值
res.append(len(temp))
temp = [nums[n2]]
# 第二个指针右移
n2 += 1
# 最后第二个指针到头,把所有临时temp写道res里
res.append(len(temp))
# 返回res中最大的数
return (max(res))
最长连续递增序列
最新推荐文章于 2024-05-19 17:14:11 发布