Problem:

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

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

Summary:

将已排序数组nums1和nums2 merge进nums1中,其中nums1和nums2的初始化元素数目分别为m和n。

默认nums1的空间大于m+n。

Solution:

1. 由于nums1有m个元素,nums2有n个元素,最终数组有m + n个元素。

我们从第m + n - 1个元素开始向前排列新数组,我所理解的原因为:

若从前往后排列数组,需要多次整体后移nums1数组中原有元素的位置,时间复杂度过低。而从后往前排列,可以在保证保留nums1未排列数组位置的情况下降低时间复杂度。

  1. class Solution {
  2. public:
  3. void merge(vector<int>& nums1, int m, vector<int>& nums2, int n) {
  4. int i = m - , j = n - ;
  5. int next = m + n - ;
  6. while (j >= ) {
  7. nums1[next--] = i >= && nums1[i] > nums2[j] ? nums1[i--] : nums2[j--];
  8. }
  9. }
  10. };

2. 首先将nums2的所有元素接在nums1后面,再用冒泡排序法对整个数组进行排序。

  1. class Solution {
  2. public:
  3. void merge(vector<int>& nums1, int m, vector<int>& nums2, int n) {
  4. for (int i = ; i < n; i++) {
  5. nums1[m + i] = nums2[i];
  6. }
  7.  
  8. int len = m + n;
  9. for (int i = ; i < len - ; i++) {
  10. for (int j = i + ; j < len; j++) {
  11. if (nums1[i] > nums1[j]) {
  12. int tmp = nums1[i];
  13. nums1[i] = nums1[j];
  14. nums1[j] = tmp;
  15. }
  16. }
  17. }
  18. }
  19. };

LeetCode 88 Merge Sorted Array的更多相关文章

  1. Leetcode#88. Merge Sorted Array(合并两个有序数组)

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

  2. [LeetCode] 88. Merge Sorted Array 混合插入有序数组

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

  3. LeetCode 88. Merge Sorted Array(合并有序数组)

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

  4. [LeetCode] 88. Merge Sorted Array 合并有序数组

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

  5. Leetcode 88. Merge Sorted Array(easy)

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

  6. [leetcode]88. Merge Sorted Array归并有序数组

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

  7. leetCode 88.Merge Sorted Array (合并排序数组) 解题思路和方法

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

  8. Leetcode 88 Merge Sorted Array STL

    合并有序数组 时间复杂度O(m+n) 该算法来自各种算法与数据结构书,写得已经烂得不能再烂了,这个应该是最短的代码了吧,不知如何归类 class Solution { public: void mer ...

  9. leetcode 88 Merge Sorted Array 归并排序

    归并排序:先将数组一分为二,将左边部分排序(同样将其一分为二),再将右边部分排序,最后逐层归并.(分治策略)(稳定排序). 算法稳定性 -- 假设在数列中存在a[i]=a[j],若在排序之前,a[i] ...

随机推荐

  1. SharePoint Fundation 2013中SecurityTokenServiceApplication错误

    在Fundation 2013与Office Web Apps Server集成,预览文档时提示错误,存入口检查失败,因为可用内存(47091712 字节)少于总内存的 5%.因此,该服务不可用于传入 ...

  2. 配置Windows下的PHP开发环境

    一.配置 Apache 开发环境: 二.配置 PHP 开发环境 配置 Apache 开发环境 0. 下载 Apache.由于官方只提供了源码包,我们要么自己编译要么使用别人提供的已经编译好的二进制包. ...

  3. 【C#】C# 队列,

    1.队列[先进先走,Dequeue():删除第一个并返回删除的这个],泛型[不确定参数的类型],

  4. 线段树 HDU 3397

    5种操作 具体看代码 #include<iostream> #include<stdio.h> #include<string.h> #include<alg ...

  5. c#使用正则表达式抓取a标签的链接和innerhtml

    //读取网页html string text = File.ReadAllText(Environment.CurrentDirectory + "//test.txt", Enc ...

  6. Configure Security Settings for Remote Desktop(RDP) Services Connections

    catalogue . Configure Server Authentication and Encryption Levels . Configure Network Level Authenti ...

  7. python远程连接paramiko 模块和堡垒机实现

    paramiko使用 paramiko模块是基于python实现了SSH2远程安全连接,支持认证和密钥方式,可以实现远程连接.命令执行.文件传输.中间SSH代理功能 安装 pip install pa ...

  8. BUAA_OVERWATCH第一次行动前战略部署

    这太IMBA了! 需求调研问卷的反馈 #define A 调查问卷 A设计背景 随着各种新兴手游的兴起,以及各大直播间内Lying Man的火热,以及各种娱乐方式的发展,传统桌游很好地移植到app上的 ...

  9. iOS提交后申请加急审核

    链接:https://developer.apple.com/appstore/contact/appreviewteam/index.html 在i would like to里选择加急审核 然后填 ...

  10. 时间戳 时区 java mysql

    当一个时间 比如2016年5月6日,生成时间戳.这个运算是与时区有关的.首先得确认这个时间是哪个时区的,然后转换成utc时区的时间.再减去1970,得到的秒数,就是时间戳. 时间戳是个一定的值,他与时 ...