题目:

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 (size that is greater or equal to m + n) to hold additional elements from B. The number of elements initialized in A and B are m and n respectively.

说明:无

实现:

精简实现:

 // 时间复杂度 O(m+n),空间复杂度 O(1)
class Solution {
public:
void merge(int A[], int m, int B[], int n) {
int i=m-,j=n-,k=m+n-;
while(i>=&&j>=)//从后面开始比较归并,直到有一个数组归并完
{
A[k--]=A[i]>B[j]?A[i--]:B[j--];//将大数赋给A[k]
}
while(j>=)//若B还没归并完,直接归并到A
A[k--]=B[j--];
}
};

leetcode 题解:Merge Sorted Array(两个已排序数组归并)的更多相关文章

  1. LeetCode第[88]题(Java):Merge Sorted Array(合并已排序数组)

    题目:合并已排序数组 难度:Easy 题目内容: Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 as ...

  2. leetcode 题解:Remove Duplicates from Sorted Array II(已排序数组去三次及以上重复元素)

    题目: Follow up for "Remove Duplicates":What if duplicates are allowed at most twice? For ex ...

  3. 两个已排序数组的合并-C语言

    最近在纸上写一个已排序数组的合并时,花了超过预期的时间.仔细想想,这种要放到毕业找工作那会两下就出来了,原因还在于工作后对基础没有重视,疏于练习. 说开一点,现在搜索引擎的发达确实给问题的解决带来了便 ...

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

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

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

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

  6. LeetCode 88 Merge Sorted Array

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

  7. LeetCode第[4]题(Java):Median of Two Sorted Arrays (俩已排序数组求中位数)——HARD

    题目难度:hard There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the median ...

  8. 【leetcode】Merge Sorted Array(合并两个有序数组到其中一个数组中)

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

  9. LeetCode(88)题解-- Merge Sorted Array

    https://leetcode.com/problems/merge-sorted-array/ 题目: Given two sorted integer arrays nums1 and nums ...

随机推荐

  1. ZOJ 1280 Interesting Lines | 求两直线交点

    原题: 求两直线交点 思路借鉴于:http://blog.csdn.net/zxy_snow/article/details/6341282 感谢大佬 #include<cstdio> # ...

  2. js实时监听input中值得变化

    <!DOCTYPE html> <html> <head> <title>zepto</title> <meta name=" ...

  3. Linux : 使用 lsof 恢复文件

    用 lsof 命令在某种程度上可以恢复删除的文件, 前提是这个文件被正在运行的进程占用. 比如: 日志文件, 配置文件. lsof 恢复文件 查找需要恢复的文件和占用文件的进程 PID lsof |g ...

  4. JS 问题整理 (1)

    监听frame加载完毕 兼容Firefox/Opera/Safari/IE的处理方式,原文链接 var oFrm = document.getElementById('ifrm'); oFrm.onl ...

  5. Linux网络编程一步一步学【转】

    转自:http://blog.chinaunix.net/uid-10747583-id-297982.html Linux网络编程一步一步学+基础  原文地址:http://blogold.chin ...

  6. 《Linux命令行与shell脚本编程大全 第3版》高级Shell脚本编程---47

    以下为阅读<Linux命令行与shell脚本编程大全 第3版>的读书笔记,为了方便记录,特地与书的内容保持同步,特意做成一节一次随笔,特记录如下:

  7. MTK_GPIO口的定制

    http://blog.csdn.net/zuoyioo7/article/details/77863291如果需要定制GPIO口呢,需要使用mediatek/dct/DrvGen.exe工具,点击O ...

  8. [MySQL] 锁/死锁问题一例

    MySQL锁/死锁问题 在MySQL中, 不同事务隔离级别下, 锁的情况表现是不同的, 另外表的设计上有无索引也是一个因素. 做一个小的实验测试InnoDB锁表现 -:) 说明 事务隔离级别 READ ...

  9. python的递归算法学习(1)

    递归函数在函数内部,可以调用其他函数.如果一个函数在内部调用自身本身,这个函数就是递归函数.举个例子,我们来计算阶乘 n! = 1 * 2 * 3 * ... * n,用函数 fact(n)表示,可以 ...

  10. POJ 3624.Charm Bracelet-动态规划0-1背包

    Charm Bracelet Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 47876   Accepted: 20346 ...