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未排列数组位置的情况下降低时间复杂度。

 class Solution {
public:
void merge(vector<int>& nums1, int m, vector<int>& nums2, int n) {
int i = m - , j = n - ;
int next = m + n - ;
while (j >= ) {
nums1[next--] = i >= && nums1[i] > nums2[j] ? nums1[i--] : nums2[j--];
}
}
};

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

 class Solution {
public:
void merge(vector<int>& nums1, int m, vector<int>& nums2, int n) {
for (int i = ; i < n; i++) {
nums1[m + i] = nums2[i];
} int len = m + n;
for (int i = ; i < len - ; i++) {
for (int j = i + ; j < len; j++) {
if (nums1[i] > nums1[j]) {
int tmp = nums1[i];
nums1[i] = nums1[j];
nums1[j] = tmp;
}
}
}
}
};

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. c#资料

    类型系统: 运行模型: 整型: 浮点: 财务: 布尔: 字符: 引用类型: Console控制字符: {序号,空间:控制字符与精度} 如:{0,3:C2} {空间:#.00} 其中,#表示该位置如果有 ...

  2. 86 ipmitools-查看硬件信息工具

    1.简介 IPMI(Intelligent Platform Management Interface)即智能平台管理接口是使硬件管理具备"智能化"的新一代通用接口标准.用户可以利 ...

  3. windows下编译chromium浏览器的15个流程整理

    编译chromium 系统为windows, 国内在windows上编译chromium的资料比较少, 我这篇文章只能作为参考, 记录我遇到的一些问题,因为chromium团队也会修改了代码,或者编译 ...

  4. phpMyAdmin 尝试连接到 MySQL 服务器,但服务器拒绝连接。您应该检查配置文件中的主机、用户名和密码

    需要修改phpmyadmin的配置文件,让其连接到MySQL数据库,用记事本打开 config.inc.php 文件 <?php /* Servers configuration */ $i = ...

  5. jQuery插件 -- Cookie插件jquery.cookie.js(转)

    Cookie是网站设计者放置在客户端的小文本文件.Cookie能为用户提供很多的使得,例如购物网站存储用户曾经浏览过的产品列表,或者门户网站记住用户喜欢选择浏览哪类新闻. 在用户允许的情况下,还可以存 ...

  6. C#FTP操作类含下载上传删除获取目录文件及子目录列表等等

    ftp登陆格式  : ftp://[帐号]:[密码]@[IP]:[端口] ftp://用户名:密码@FTP服务器IP或域名:FTP命令端口/路径/文件名 直接上代码吧,根据需要选择函数,可根据业务自己 ...

  7. Linux解压,压缩小总结

    linux下打包与解压的三种命令 最近在读<鸟歌的Linux私房菜基础篇>,想着总结一下所读知识,有益于理解. Linux下常用的命令有三种 gzip,zcat(用于zip,gzip等) ...

  8. [Nhibernate]体系结构

    引言 在项目中也有用到过nhibernate但对nhibernate的认识,也存留在会用的阶段,从没深入的学习过,决定对nhibernate做一个系统的学习. ORM 对象-关系映射(OBJECT/R ...

  9. [NHibernate]组件之依赖对象

    目录 写在前面 文档与系列文章 组件之依赖对象 一个例子 总结 写在前面 周一至周四一直在成都出差,也一直没有更新博客了,一回到家第一件事就是扒一扒最近博客园更新的文章,然后把想看的收藏了,大概有20 ...

  10. [Centos 6]升级安装GCC(2)

    摘要 上篇文章升级了下gcc,但发现并没有起到作用. 安装 上篇文章: 升级GCC 升级之后,检查gcc版本 strings /usr/lib/libstdc++.so. | grep GLIBCXX ...