列表中最长的递增顺序

时间:2019-07-13 04:34:09

标签: python-3.x

在python中,如果您有类似[3,5,1,66,99,134,5,6]的列表,则应将其拆分为[[3,5],[1,66,99,134],[5,6] ]],最后应返回[1,66,99,134]列表。无需使用函数和类即可将其用于任何列表(动态)

numbers = []
n = int(input("Enter How many numbers:"))
for i in range(0, n):
    numbers.append(int(input("Enter Values Of List:")))
print(numbers)

temp = [1 for x in range(0, n)]

i = 1
j = 0
count = 0

while (i <length(numbers) and j < length(numbers)):
    if numbers[j] < numbers[i]:
       if temp[j] + 1 > temp[i]:
          temp[i] = temp[j]+1
    if j == i:
        j, i = 0, i + 1
    count += 1
    if count == n:
        break
print(max(numbers))

1 个答案:

答案 0 :(得分:0)

ls = []
ls1 = []
ls2 = [ls1]
num = int(input("Enter How many numbers:"))
for n in range(num):
    numbers = int(input("Enter Numbers:"))
    ls.append(numbers)
print('List is:', ls, '\n')

for i in ls:
    if len(ls1) == 0:
        ls1.append(i)
    elif ls2[-1][-1] < i:
        ls2[-1].append(i)
    else:
        ls2.append([i])
print(ls2)

ls3 = []
for i in ls2:
    ls3.append(len(i))
print('Longest Increasing Sequence is:', ls2[ls3.index(max(ls3))])