题目来源:
https://leetcode.com/problems/merge-sorted-array/
题意分析:
给定两个排好序的数组nums1和nums2,将两个数组整合成一个新的排好序的数组,并将这个数组存在nums1里面。
题目思路:
由于题目没有要求,所以用一个tmp临时变量将nums1和nums2的数组整合起来,然后将tmp的数赋给nums1就可以了。
代码(Python):
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
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