问题

该文章的最新版本已迁移至个人博客【比特飞】,单击链接 https://www.byteflying.com/archives/3686 访问。

给定两个有序整数数组 nums1 和 nums2,将 nums2 合并到 nums1 使得 num1 成为一个有序数组。

说明:

  • 初始化 nums1 和 nums2 的元素数量分别为 m 和 n
  • 你可以假设 nums1 有足够的空间(空间大小大于或等于 m + n)来保存 nums2 中的元素。

输入:

nums1 = [1,2,3,0,0,0], m = 3

nums2 = [2,5,6],       n = 3

输出: [1,2,2,3,5,6]


Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 as one sorted array.

Note:

  • The number of elements initialized in nums1 and nums2 are m and n respectively.
  • You may assume that nums1 has enough space (size that is greater or equal to m + n) to hold additional elements from nums2.

Input:

nums1 = [1,2,3,0,0,0], m = 3

nums2 = [2,5,6],       n = 3

Output: [1,2,2,3,5,6]


示例

该文章的最新版本已迁移至个人博客【比特飞】,单击链接 https://www.byteflying.com/archives/3686 访问。

public class Program {

    public static void Main(string[] args) {
int[] nums1 = { 1, 3, 5, 7, 9, 0, 0, 0, 0, 0 };
int[] nums2 = { 2, 4, 6, 8, 10 }; Merge(nums1, 5, nums2, 5);
ShowArray(nums1); Console.ReadKey();
} private static void ShowArray(int[] array) {
foreach(var num in array) {
Console.Write($"{num} ");
}
Console.WriteLine();
} private static void Merge(int[] nums1, int m, int[] nums2, int n) {
//双指针法扫描数组
int point1 = m - 1;
int point2 = n - 1;
//指针,试图向第1个数组从后往前填充数值
int index = m + n - 1;
//从后往前扫描双数组
while(point1 >= 0 && point2 >= 0) {
//如果数组1中的值大,则将数组1中的数据放入数组1中的后面部分
if(nums1[point1] > nums2[point2]) {
nums1[index--] = nums1[point1--];
//否则,则将数组2中的数据放入数组1中的后面部分
} else {
nums1[index--] = nums2[point2--];
}
}
//当数组2中的值提前结束时,复制数组1中余下的部分
while(point1 >= 0) {
nums1[index--] = nums1[point1--];
}
//当数组1中的值提前结束时,复制数组2中余下的部分
while(point2 >= 0) {
nums1[index--] = nums2[point2--];
}
} }

以上给出1种算法实现,以下是这个案例的输出结果:

该文章的最新版本已迁移至个人博客【比特飞】,单击链接 https://www.byteflying.com/archives/3686 访问。

1 2 3 4 5 6 7 8 9 10

分析:

显而易见,以上参考算法在最坏的情况下的时间复杂度为:  。

C#LeetCode刷题之#88-合并两个有序数组(Merge Sorted Array)的更多相关文章

  1. 高效合并两个有序数组(Merge Sorted Array)

    Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 as one sorted array. Note: Y ...

  2. [Swift]LeetCode88. 合并两个有序数组 | Merge Sorted Array

    Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 as one sorted array. Note: T ...

  3. [LeetCode每日一题]88. 合并两个有序数组

    [LeetCode每日一题]88. 合并两个有序数组 问题 给你两个有序整数数组 nums1 和 nums2,请你将 nums2 合并到 nums1 中,使 nums1 成为一个有序数组. 初始化 n ...

  4. Java实现 LeetCode 88 合并两个有序数组

    88. 合并两个有序数组 给定两个有序整数数组 nums1 和 nums2,将 nums2 合并到 nums1 中,使得 num1 成为一个有序数组. 说明: 初始化 nums1 和 nums2 的元 ...

  5. 【LeetCode】88. 合并两个有序数组

    88. 合并两个有序数组 知识点:数组:排序:双指针: 题目描述 给你两个按 非递减顺序 排列的整数数组 nums1 和 nums2,另有两个整数 m 和 n ,分别表示 nums1 和 nums2 ...

  6. Leetcode 88 合并两个有序数组 Python

    合并两个有序数组 给定两个有序整数数组 nums1 和 nums2,将 nums2 合并到 nums1 中,使得 num1 成为一个有序数组. 说明: 初始化 nums1 和 nums2 的元素数量分 ...

  7. leetcode刷题-88.合并两个有序数组

    题目 给你两个有序整数数组 nums1 和 nums2,请你将 nums2 合并到 nums1 中,使 nums1 成为一个有序数组. 说明: 初始化 nums1 和 nums2 的元素数量分别为 m ...

  8. LeetCode 88. 合并两个有序数组

    题目: 给定两个有序整数数组 nums1 和 nums2,将 nums2 合并到 nums1 中,使得 num1 成为一个有序数组. 说明: 初始化 nums1 和 nums2 的元素数量分别为 m ...

  9. LeetCode 88. 合并两个有序数组(Merge Sorted Array)

    题目描述 给定两个有序整数数组 nums1 和 nums2,将 nums2 合并到 nums1 中,使得 num1 成为一个有序数组. 说明: 初始化 nums1 和 nums2 的元素数量分别为 m ...

随机推荐

  1. Database Identifiers - SID

    These options include your global database name and system identifier (SID). The SID is a unique ide ...

  2. ajax原生js封装

    不带注释的 function ajax(json) { json.type = json.type ? json.type : 'get'; json.async = json.async == fa ...

  3. echarts 踩坑 : 为什么效果出不来?看看有没有正确引入

    今天我要给 echarts 组件加个 dataZoom 功能,结果发现没有效果. 后来发现是引入 echarts 模块的问题. 如果是按需引入的话,必须引入相应的功能模块才能使用相应的功能. 举例: ...

  4. 使用Azure DevOps Pipeline实现.Net Core程序的CI

    上次介绍了Azure Application Insights,实现了.net core程序的监控功能.这次让我们来看看Azure DevOps Pipeline功能.Azure DevOps Pip ...

  5. html命名规则

    CSS样式命名 外套 wrap ------------------用于最外层 头部 header ----------------用于头部 主要内容 main ------------用于主体内容( ...

  6. SSM框架整合的最新打开方式(打造最详细的SSM整合教程)

    SSM整合 文章已托管到GitHub,大家可以去GitHub查看阅读,欢迎老板们前来Star!搜索关注微信公众号 [码出Offer] 领取各种学习资料! SSM 一.创建一个Maven项目 File ...

  7. shell 格式化数据,转换为execl

    awk '  BEGIN { OFS="\t"} ;{ $1=$1 ; print $8,$NF} ' >/root/log/aa.xlsx awk '  BEGIN { O ...

  8. Docker 挂载

    简介   集群当中挂载数据卷的方式采用--mount标志.而且-mount标记相比于-v意图更明确.   如果不进行数据挂载的话,当容器不在时,对应的数据也不会持久存在 存储方式 卷存储,由docke ...

  9. PHP scandir() 函数

    ------------恢复内容开始------------ 实例 列出 images 目录中的文件和目录: <?php$dir = "/images/"; // Sort ...

  10. PHP round() 函数

    实例 对浮点数进行四舍五入:高佣联盟 www.cgewang.com <?php echo(round(0.60) . "<br>"); echo(round(0 ...