合并有序数组

class Solution:
#参数A:有序整数数组A
#参数B:有序整数数组B
#返回:一个新的有序整数数组

def merge(self, A, B):
    i,j = 0,0
    c=[]
    while i<len(A) and j<len(B):
        if A[i]<B[j]:
            c.append(A[i])
            i+=1
        else:
            c.append(B[j])
            j+=1
    while i < len(A):
        c.append(A[i])
        i+=1
    while j < len(B):
        c.append(B[j])
        j += 1
    return c

if name == ‘main’:
A=[1,2,3]
B=[1,4,5,6]
solution = Solution()
print(“输入:”,A," ",B)
print(“输出:”, solution.merge(A,B))
合并有序数组

上一篇:C#应用程序文件类型


下一篇:腾讯五十题 No.41 2的幂