首页 > 其他 > 详细

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

时间:2018-03-28 12:59:08      阅读:164      评论:0      收藏:0      [点我收藏+]
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(从后向前复制)

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

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!