老爷子88 发表于 2017-5-6 08:07:40

轩辕互动面试题目-----最大递增子序列(Python实现)

数组A中存放很多数据,比如A={1,2,3,4,3,2,1,4,8,9,10};其中1,2,3,4/1,4,8,9,10都是递增子序列,1,4,8,9,10是最长的递增子序列。
寻找数组中的最长子序列,返回起始的索引值,如果没有递增子序列,那么返回-1.
实际就是连续判断A是否比A大,下面是我的代码:
代码

def function(li):
length = len(li)
count = 1
max = 0
i = 0
first = 0
while True:
j = i + 1
if li > li:
count += 1
else:
first = first + 1
if first == 1:
max = count
start = 0
else:
temp = j
if max < count:
max = count
start = temp
count = 1
i += 1
if j == length -1:
if max < count:
max = count
if max < 1:
return -1
return (start,max)
if __name__ == '__main__':
li =
print function(li)

如果大家又更好的方法,不吝赐教。
页: [1]
查看完整版本: 轩辕互动面试题目-----最大递增子序列(Python实现)