zoukankan      html  css  js  c++  java
  • [LeetCode]题解(python):088-Merge Sorted Array

    题目来源:

      https://leetcode.com/problems/merge-sorted-array/


    题意分析:

      给定两个排好序的数组nums1和nums2,将两个数组整合成一个新的排好序的数组,并将这个数组存在nums1里面。


    题目思路:

      由于题目没有要求,所以用一个tmp临时变量将nums1和nums2的数组整合起来,然后将tmp的数赋给nums1就可以了。


    代码(Python):

      

     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: void Do not return anything, modify nums1 in-place instead.
     9         """
    10         tmp = []
    11         i,j = 0,0
    12         while i < m or j < n:
    13             if i != m and j != n:
    14                 if nums1[i] < nums2[j]:
    15                     tmp.append(nums1[i]);i += 1
    16                 else:
    17                     tmp.append(nums2[j]);j += 1
    18             elif i == m:
    19                 tmp.append(nums2[j]); j += 1
    20             else:
    21                 tmp.append(nums1[i]); i += 1
    22         i = 0
    23         while i < (m + n):
    24             nums1[i] = tmp[i]
    25             i += 1
    26         
    View Code

    转载请注明出处:http://www.cnblogs.com/chruny/p/5088666.html 

  • 相关阅读:
    16
    15
    14
    13
    12
    11
    10
    python包管理器修改镜像地址
    Linux环境下安装hadoop分布式集群+问题总结
    解剖css中的clear属性
  • 原文地址:https://www.cnblogs.com/chruny/p/5088666.html
Copyright © 2011-2022 走看看