题目描述:给定一串数组,返回其中的最长递增子序列的长度。例如:给定数组[10, 9, 2, 5, 3, 7, 101, 18],则其最长递增子序列为[2, 3, 7, 101],返回长度4
解题思路:用i来遍历从第二个元素开始,用j来便利i前面的数,判断J指向的元素是否小于i指向的元素?如果成立,则j对应的L序列+1和当前的i相比取大者,以此类推
10 | 9 | 2 | 5 | 3 | 7 | 101 | 18 | |
L | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 |
2 | 1 | 1 | 1 | 1 | ||||
2 | 1 | 1 | 1 | |||||
3 | 1 | 1 | ||||||
3 | 1 | |||||||
4 | 1 | |||||||
4 | ||||||||
class Solution:
def lengthOfLIS(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
leng = len(nums)
if leng==0:
return 0
L = [1]*leng
for i in range(1,leng):
for j in range(i):
if nums[i]>nums[j]:
L[i]=max(L[j]+1,L[i])
return max(L)