One - One Code All

Blog Content

Lintcode-6:合并排序数组 II

Python 每日一练 算法 LintCode   2010-03-06 10:05:16

描述
合并两个排序的整数数组A和B变成一个新的数组。
 
样例
给出A=[1,2,3,4],B=[2,4,5,6],返回 [1,2,2,3,4,4,5,6]
挑战
你能否优化你的算法,如果其中一个数组很大而另一个数组很小?

def sort1(list1,list2):
    len1 = len(list1)
    len2 = len(list2)

    list3 = []

    i = 0
    j = 0
    k = 0

    while (i < len1 and j < len2 ):
        if list1[i] < list2[j]:           
            list3.append(list1[i])
            i = i + 1
        else:
            list3.append(list2[j])
            j = j + 1


    while(i


结果输出:

[1, 2, 2, 3, 4, 4, 5, 6]


上一篇:Lintcode-5:在数组中找到第k大的元素
下一篇:Lintcode-7:二叉树的序列化和反序列化

The minute you think of giving up, think of the reason why you held on so long.