思路:以1、3、5、4、7为例,从i=1开始循环,当后面第j个数开始不递增时,作为i = 1的递增长度。当i = k时,第k个数的递增长度大于数组长度一半以上时,直接返回i = k时的对应递增长度。如果不大于数组的一半时,则找到所有递增长度的最大值。
class Solution(object):
def findLengthOfLCIS(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
num = 1
if not (nums):
return 0
if(len(nums) == 1):
return 1;
for i in range(len(nums)):
num1 = 0;
for j in range(i, len(nums)):
if i == j:
num1 = num1 + 1
continue
if nums[j] > nums[j-1]:
num1 = num1 + 1
else:
break
if((num1 > num) & (num1 > float(len(nums))/2)):
num = num1
return num1
elif((num1 > num) & (num1 <= (float(len(nums))/2))):
num = num1;
return num
解题结果: