zoukankan      html  css  js  c++  java
  • 【leetcode】Merge Sorted Array

    题目描述

    Given two sorted integer arrays A and B, merge B into A as one sorted array.

    Note:
    You may assume that A has enough space (size that is greater or equal to m + n) to hold additional elements from B. The number of elements initialized in A and B are m and n respectively.

    解题思路:

    class Solution:
        # @param A  a list of integers
        # @param m  an integer, length of A
        # @param B  a list of integers
        # @param n  an integer, length of B
        # @return nothing(void)
        def merge(self, A, m, B, n):
            posa = m - 1
            posb = n - 1
            for i in range(m+n-1,-1,-1):
                if posa < 0:
                    A[i] = B[posb]
                    posb -= 1
                elif posb < 0:
                    return
                elif A[posa] > B[posb]:
                    A[i] = A[posa]
                    posa -= 1
                else:
                    A[i] = B[posb]
                    posb -= 1
  • 相关阅读:
    C#基础
    进制转换
    养猪和存储空间
    独热码和二进制码
    mux_xz
    饮料机
    亚稳态
    mos管功耗
    功能覆盖率和代码覆盖率
    时序逻辑电路输出特点
  • 原文地址:https://www.cnblogs.com/MrLJC/p/4437815.html
Copyright © 2011-2022 走看看