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 

  • 相关阅读:
    代码互改
    第一次个人编程作业
    第一次博客
    个人总结
    第三次个人作业--用例图设计
    第二次结对作业
    第一次结对作业
    记录浏览他人代码
    中文编程作业
    第一篇随笔
  • 原文地址:https://www.cnblogs.com/chruny/p/5088666.html
Copyright © 2011-2022 走看看