码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2018-03-28 14:15:38      阅读:149      评论:0      收藏:0      [点我收藏+]

标签:sel   zed   log   void   list   rom   ber   长度   tab   

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

 

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

标签:sel   zed   log   void   list   rom   ber   长度   tab   

原文地址:https://www.cnblogs.com/zle1992/p/8662849.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!