「LeetCode每日一题」——LCCI 10.01. 合并排序的数组
LCCI 10.01. 合并排序的数组
链接:https://leetcode-cn.com/problems/sorted-merge-lcci/
难度:简单
题目
给定两个排序后的数组 A 和 B,其中 A 的末端有足够的缓冲空间容纳 B。 编写一个方法,将 B 合并入 A 并排序。
初始化 A 和 B 的元素数量分别为 m 和 n。
示例:
输入:
A = [1,2,3,0,0,0], m = 3
B = [2,5,6], n = 3
输出: [1,2,2,3,5,6]
思路
这个题的思路主要是有两个。第一种就是双指针,从后往前扫描,较大的往后放,直到A数组遍历完,然后数组B就直接复制到数组A。第二种就是加起来排序,当然Python写第二种更简单。具体代码请见下面方案代码。
方案代码
解法:
class Solution:
def merge(self, A: List[int], m: int, B: List[int], n: int) -> None:
"""
Do not return anything, modify A in-place instead.
"""
A[m:] = B
A.sort()
相关
原创文章,作者:flypython,如若转载,请注明出处:http://flypython.com/algorithm/leetcode/162.html
相关推荐
-
「LeetCode每日一题」—— 466. 统计重复个数
466. 统计重复个数 链接:https://leetcode-cn.com/problems/count-the-repetitions/难度:困难 题目 思路 这是一道困难题,…
19/04/2020 -
「LeetCode每日一题」——300. 最长上升子序列
300. 最长上升子序列 链接:https://leetcode-cn.com/problems/longest-increasing-subsequence/难度:中等 题目 给…
14/03/2020 -
「LeetCode每日一题」——892. 三维形体的表面积
892. 三维形体的表面积 链接:https://leetcode-cn.com/problems/surface-area-of-3d-shapes/难度:简单 题目 在&nbs…
25/03/2020 -
「LeetCode每日一题」—— 33. 搜索旋转排序数组
33. 搜索旋转排序数组 链接:https://leetcode-cn.com/problems/search-in-rotated-sorted-array/难度:中等 题目 点…
27/04/2020 -
「LeetCode每日一题」——1162. 地图分析
1162. 地图分析 链接:https://leetcode-cn.com/problems/as-far-from-land-as-possible/难度:中等 题目 你现在手里…
29/03/2020 -
「LeetCode每日一题」——LCCI.17.16. 按摩师
LCCI.17.16. 按摩师 链接:https://leetcode-cn.com/problems/the-masseuse-lcci/难度:简单 题目 一个有名的按摩师会收到…
24/03/2020 -
「LeetCode每日一题」—— 542. 01 矩阵
542. 01 矩阵 链接:https://leetcode-cn.com/problems/01-matrix/难度:中等 题目 点击原文链接跳转查看题目 思路 这道题求的就是每…
15/04/2020 -
「LeetCode每日一题」——876. 链表的中间结点
876. 链表的中间结点 链接:https://leetcode-cn.com/problems/middle-of-the-linked-list/难度:简单 题目 给定一个带有…
23/03/2020 -
「LeetCode每日一题」—— 46. 全排列
46. 全排列 链接:https://leetcode-cn.com/problems/permutations/难度:中等 题目 点击原文链接跳转查看题目 思路 这题算是一个基础…
25/04/2020 -
「LeetCode每日一题」——543. 二叉树的直径
543. 二叉树的直径 链接:https://leetcode-cn.com/problems/diameter-of-binary-tree/难度:简单 题目 给定一棵二叉树,你…
10/03/2020
您必须登录才能发表评论。