88. 合并两个有序数组

https://leetcode-cn.com/problems/merge-sorted-array/

Java

/*
 * @Author: Goog Tech
 * @Date: 2020-08-24 08:55:36
 * @LastEditTime: 2020-08-24 09:08:00
 * @Description: https://leetcode-cn.com/problems/merge-sorted-array/
 * @FilePath: \leetcode-googtech\#88. Merge Sorted Array\Solution.java
 * @WebSite: https://algorithm.show/
 */

/*
 * @lc app=leetcode.cn id=88 lang=java
 *
 * [88] 合并两个有序数组
 */

// @lc code=start
class Solution {

    // 先合并后排序: 但这种方法没有利用两个数组本身已经有序这一点
    public void merge(int[] nums1, int m, int[] nums2, int n) {
        // arraycopy(Object src,int srcPos,Object dest, int destPos,int length);
        System.arraycopy(nums2, 0, nums1, m , n);
        Arrays.sort(nums1);
    }
}
// @lc code=end

Python

'''
Author: Goog Tech
Date: 2020-08-24 08:49:22
LastEditTime: 2020-08-24 09:03:04
Description: https://leetcode-cn.com/problems/merge-sorted-array/
FilePath: \leetcode-googtech\#102. Binary Tree Level Order Traversal\88.合并两个有序数组.py
WebSite: https://algorithm.show/
'''

#
# @lc app=leetcode.cn id=88 lang=python
#
# [88] 合并两个有序数组
#

# @lc code=start
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.
        """
        # nums1[:] 等价于 nums1[0 : len(nums1)] 相当于取 nums1 对应的对象的一个视图
        nums1[0 : len(nums1)] = sorted(nums1[:m] + nums2[:n])
# @lc code=end
Copyright © GoogTech 2021 all right reserved,powered by GitbookLast update time : 2021-09-15 01:55:05

results matching ""

    No results matching ""