`
edison0951
  • 浏览: 70621 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

轩辕互动面试题目-----最大递增子序列(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[i]是否比A[i-1]大,下面是我的代码:
代码
def function(li):
    length = len(li)
    count = 1 
    max = 0 
    i = 0 
    first = 0 
    while True:
        j = i + 1 
    
        if li[j] > li[i]:
            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 = [1,4,3,5,7,2,11,15,21]
    print function(li)

如果大家又更好的方法,不吝赐教。
0
0
分享到:
评论
2 楼 edison0951 2010-02-21  
没有,只是觉得算法有点意思,玩一玩。
1 楼 charlesgong 2010-02-20  
老兄你在EXOWEB?

相关推荐

Global site tag (gtag.js) - Google Analytics