<插入排序>
给你两个有序整数数组 nums1 和 nums2,请你将 nums2 合并到 nums1 中,使 num1 成为一个有序数组。
说明:
示例:
输入: nums1 = [1,2,3,0,0,0], m = 3 nums2 = [2,5,6], n = 3 输出: [1,2,2,3,5,6]
选择排序。
1.已知 nums1 是已经排好序的数组
2.我们从 nums2 中顺序选择元素插入到 nums1 中合适的位置(像冒泡一样把要 nums2 中选择的元素放到合适的位置)
class Solution(object): def merge(self, nums1, m, nums2, n): """ :type nums1: List[int] :type m: int :type nums2: List[int] :type n: int :rtype: None Do not return anything, modify nums1 in-place instead. """ for i in range(len(nums2)): j = m+i-1 key = nums2[i] while j>=0 and nums1[j]>key: nums1[j+1] = nums1[j] j-=1 nums1[j+1] = key
1. 插入排序
原文:https://www.cnblogs.com/remly/p/12519430.html