问题

该文章的最新版本已迁移至个人博客【比特飞】,单击链接 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. static关键字有何魔法?竟让Spring Boot搞出那么多静态内部类

    生命太短暂,不要去做一些根本没有人想要的东西.本文已被 https://www.yourbatman.cn 收录,里面一并有Spring技术栈.MyBatis.JVM.中间件等小而美的专栏供以免费学习 ...

  2. 软件测试中的微信小程序怎么测试?

    1.没有需求文档时,如何测试小程序?现在大多数公司的开发模式是:敏捷模式(用户故事) ,即以什么身份做什么事情会出现什么样的结果.那实际测试过程中,没有需求文档时,测试可以采用以下方式更好的完成测试工 ...

  3. C++11多线程编程--线程创建

    参考资料 adam1q84 我是一只C++小小鸟 Thread support library Book:<C++ Concurrency in Action> 线程的创建 线程的创建有多 ...

  4. MemoryCacheHelper与RedisCacheHelper缓存集成与测试笔记

    因为每次在新项目中需要花费大量时间在基础类库搬移.调试.为了节省时间(偷懒)就将MemoryCacheHelper/RedisHelper进行了封装 本次是关于缓存方面记录,源码请参考(包含Redis ...

  5. webpack的使用 一、webpack 和webpack的安装

    本质上,webpack 是一个现代 JavaScript 应用程序的静态模块打包器(module bundler).当 webpack 处理应用程序时, 它会递归地构建一个依赖关系图(dependen ...

  6. HRNet + Object Contextual Representation

    文章内容来自CCF-CV走进高校报告会中MSRA王井东老师的报告"Learning high-resolution and object-contextual representations ...

  7. Object#wait()与Object#wait(long)的区别

    例子 例1 最基础的等待-通知 下面一个例子,一个线程"waiting"在同步代码块调用了Object#wait()方法,另一个线程"timedWaiting" ...

  8. Html5 表单元素基础

    表单元素 1.定义: 表单是提供让读者在网页上输入,勾选和选取数据,以便提交给服务器数据库的工具.(邮箱注册,用户登录,调查问卷等) 2.表单元素(下拉框,输入框……) 3.表单主结构: <fo ...

  9. manual for emacs markdown-mode(English)

    markdown-mode now requires Emacs 24.3 or later. Markup insertion and replacement keybindings under C ...

  10. Ribbon 负载规则替换

    1 添加规则类: 注意: 官方文档明确给出了警告: 这个自定义配置类不能放在 @ComponentScan 所扫描的当前包下以及子包下,否则自定义的配置类就会被所有的 Ribbon 客户端所共享,达不 ...