zoukankan      html  css  js  c++  java
  • 88. 合并两个有序数组

       

    思路:
      指针i和j分别遍历nums1和nums2;
      取两指针较小者追加到res中,较小指针后移,较大者不动;
      若两指针相等,则两者都追加到res中,两指针均后移;
      i<m或j<n时,停止遍历,将两串之一剩余的部分有序序列追加到res中。
    注:本题提交的时候,程序不用return,另外本题必须在nums1原址上做合并操作。

    下面写了三个方法,第三个是通过的。
     1 class Solution(object):
     2     def merge(self, nums1, m, nums2, n):
     3         """
     4         :type nums1: List[int]
     5         :type m: int
     6         :type nums2: List[int]
     7         :type n: int
     8         :rtype: None Do not return anything, modify nums1 in-place instead.
     9         """
    10         i = j = 0
    11         while j < n and i < m:
    12             if nums1[i] < nums2[j]:
    13                 i += 1
    14             elif nums1[i] == nums2[j]:
    15                 i += 1
    16                 nums1.insert(i, nums2[j])
    17                 i += 1
    18                 j += 1
    19                 m += 1
    20             elif nums1[i] > nums2[j]:
    21                 if i == 0:
    22                     nums1.insert(i, nums2[j])
    23                 else:
    24                     nums1.insert(i - 1, nums2[j])
    25                 j += 1
    26         if i == m and j < n:
    27             while j < n:
    28                 nums1[i] = nums2[j]
    29                 i += 1
    30                 j += 1
    31         while nums1[-1] == 0:
    32             nums1.pop(-1)
    33         return nums1
    34 
    35     def merge2(self, nums1, m, nums2, n):
    36         """
    37         :type nums1: List[int]
    38         :type m: int
    39         :type nums2: List[int]
    40         :type n: int
    41         :rtype: None Do not return anything, modify nums1 in-place instead.
    42         """
    43         nums1 = nums1[0:m] + nums2
    44         nums1.sort()
    45         return nums1
    46 
    47     def merge3(self, nums1, m, nums2, n):
    48         """
    49         :type nums1: List[int]
    50         :type m: int
    51         :type nums2: List[int]
    52         :type n: int
    53         :rtype: None Do not return anything, modify nums1 in-place instead.
    54         """
    55         i, j = -1, 0
    56         while j < len(nums2):
    57             nums1[i] = nums2[j]
    58             i -= 1
    59             j += 1
    60         nums1.sort()
    61         return nums1
    62 
    63 
    64 if __name__ == '__main__':
    65     solution = Solution()
    66     print(solution.merge3(nums1=[1, 2, 3, 0, 0, 0], m=3, nums2=[2, 5, 6], n=3))
    67     # print(solution.merge3(nums1=[2, 0], m=1, nums2=[1], n=1))
     
  • 相关阅读:
    some tips
    ORA00847: MEMORY_TARGET/MEMORY_MAX_TARGET and LOCK_SGA cannot be set together
    Chapter 01Overview of Oracle 9i Database Perfomrmance Tuning
    Chapter 02Diagnostic and Tuning Tools
    变量与常用符号
    Chapter 18Tuning the Operating System
    标准输入输出
    Trace files
    DBADeveloped Tools
    Chapter 03Database Configuration and IO Issues
  • 原文地址:https://www.cnblogs.com/panweiwei/p/12723166.html
Copyright © 2011-2022 走看看