• 88. Merge Sorted Array(从后向前复制)


    Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 as one sorted array.

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

    nums1已经是m+n长度了,不用新申请空间。

    从后向前复制。

     1 class Solution:
     2     def merge(self, l1, m, l2, n):
     3         """
     4         :type nums1: List[int]
     5         :type m: int
     6         :type nums2: List[int]
     7         :type n: int
     8         :rtype: void Do not return anything, modify nums1 in-place instead.
     9         """
    10         i = m - 1
    11         j = n - 1
    12 
    13         end = m+n-1
    14         while(i >= 0 and j >= 0):
    15             if(l1[i] > l2[j]):
    16                 l1[end] = l1[i]
    17                 i-=1
    18             else:
    19                 l1[end] = l2[j]
    20                 j -= 1
    21             end -=1
    22 
    23         while (j>=0):
    24             l1[end] = l2[j]
    25             j -= 1
    26             end-=1
  • 相关阅读:
    记一道有趣的数学题
    BJOI2018 二进制
    BJOI2016 IP地址
    BJOI2016 回转寿司
    BJOI2017 开车
    BJOI2019 光线
    java 下载
    springboot 运行相关命令
    sql mapper 里面 Integer 类型判断
    springboot 访问jar同级别下的文件访问问题
  • 原文地址:https://www.cnblogs.com/zle1992/p/8662849.html
Copyright © 2020-2023  润新知