Given two sorted integer arrays A and B, merge B into A as one sorted array.

Note:
You may assume that A has enough space to hold additional elements from B. The number of elements initialized in A and B are m and n respectively.

考虑从后往前比较,这样就不会产生需要数据后移的问题了。时间复杂度O(n+m)

 1 class Solution {
 2 public:
 3     void merge(int A[], int m, int B[], int n) {
 4         // Start typing your C/C++ solution below
 5         // DO NOT write int main() function
 6         int index = m + n - 1;
 7         int aIndex = m - 1;
 8         int bIndex = n - 1;
 9         while(0 <= aIndex && 0 <= bIndex)
10         {
11             if (B[bIndex] > A[aIndex])
12             {
13                 A[index--] = B[bIndex--];
14             }
15             else
16             {
17                 A[index--] = A[aIndex--];
18             }
19         }
20
21         while(0 <= aIndex)
22         {
23             A[index--] = A[aIndex--];
24         }
25
26         while(0 <= bIndex)
27         {
28             A[index--] = B[bIndex--];
29         }
30     }
31 };

转载于:https://www.cnblogs.com/chkkch/archive/2012/11/16/2772935.html

[LeetCode] Merge Sorted Array相关推荐

  1. [Leetcode] Merge Sorted Array 合并数组

    Merge Sorted Array 最新更新请见:https://yanjia.me/zh/2019/02/... Given two sorted integer arrays nums1 and ...

  2. [LeetCode]Merge Sorted Array

    题目:给定两个int型有序数组A和B,将B数组合并到A数组中(A数组空间足够) 算法:模拟插入排序,从右往左推断并往A数组中插入B数组元素 public class Solution {public ...

  3. Leetcode Merge Sorted Array

    合并排序后的数组 方法:从长数组的尾部开始,可以时间复杂度O(m+n),空间复杂度O(1) class Solution { public:void merge(int A[], int m, int ...

  4. 辣鸡刘的Leetcode之旅8【Add Binary,Sqrt(x),Merge Sorted Array】

    Add Binary 题目描述; Given two binary strings, return their sum (also a binary string). The input string ...

  5. leetcode 88 Merge Sorted Array

    lc88 Merge Sorted Array 因为两个array都已经排好序了,每次取两者间较小的放入新array中,当一个array空了,直接把另一个array剩下的全放入新array即可 1 c ...

  6. LeetCode之Merge Sorted Array

    1.问题 Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 as one sorted array. No ...

  7. leetcode python3 简单题88. Merge Sorted Array

    1.编辑器 我使用的是win10+vscode+leetcode+python3 环境配置参见我的博客: 链接 2.第八十八题 (1)题目 英文: Given two sorted integer a ...

  8. LeetCode 之 Merge Sorted Array(排序)

    [问题描写叙述] Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 as one sorted array ...

  9. 【LeetCode从零单排】No88.Merge Sorted Array

    题目 Given two sorted integer arrays A and B, merge B into A as one sorted array. Note: You may assume ...

最新文章

  1. java超级简单到爆的Excel导入导出(easypoi)
  2. 有关定时任务的表达式--cron 详细解
  3. 设计包含min函数的栈
  4. 使用ajax将数据显示在指定位置_AJAX学习主题之一
  5. Python之celery的简介与使用
  6. html盒子模型子元素怎么水平占满父元素_前端面试常考问题之css盒模型
  7. MQTT协议之发布订阅
  8. AJAX vs FLEX:执行,传送,解析JSON,HTML,XML,AFM格式效率比较.
  9. jQuery修改margin
  10. php:页面乱码的解决方法
  11. Linux的软链接和硬链接
  12. 帝骑k触屏模拟器_假面骑士decade神主牌模拟器手机版下载|
  13. 集成腾讯信鸽推送遇到的大坑
  14. 解决其他浏览器能上网谷歌浏览器不能上网
  15. 卷积神经网络——灰度图像的卷积
  16. 【论文阅读】Causality matters in medical imaging
  17. AI转型中的思考和洞见
  18. 6-7-Isomorphic-函数题
  19. Nginx 配置 HTTPS 证书
  20. 为某一目录创建Internet来宾账户

热门文章

  1. js改变style中的值
  2. python中绝对角度是什么意思_Python中的角度转换功能
  3. 服务链路追踪配置mysql_学习微服务的服务链路追踪——Spring Cloud Sleuth+zipkin
  4. python引入redis_实操演练解读非关系型数据库—Redis
  5. 轻量化html编辑器,web端实现富文本编辑器
  6. windows mac linux 木马,针对Linux Windows macOS系统Adwind木马广告攻击
  7. php图片翻转函数,PHP图片处理之图片旋转和图片翻转实例
  8. asp.net mvc redis同步mysql_Mysql和Redis数据同步策略 - 元思 - 博客园
  9. Typore快速插入图片
  10. PAT-B wifi密码(非常巧妙)