题目:

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. CentOS7 安装 webgoat 7.1 简介

    CentOS7 安装 webgoat 7.1 简介 webgoat 所需文件准备: 操作系统版本:CentOS 7.3 1: 在Linux上安装Openjdk >= 1.8 2: 上传文件至 L ...

  2. joyOI 选课 【树形dp + 背包dp】

    题目链接 选课 题解 基础背包树形dp #include<iostream> #include<cstdio> #include<cmath> #include&l ...

  3. 商店购物 (shopping.c/cpp/pas)

    1.商店购物 (shopping.c/cpp/pas) 在滨海市开着 n 家商店,编号依次为 1 到 n,其中编号为 1 到 m 的商店有日消费量上 限,第 i 家商店的日消费量上限为 wi. 海霸王 ...

  4. 使用jdbc连接oracle数据库时遇到的一些问题

    第一个问题:驱动名称错误 错误截图如下: 从错误提示可以看出,oracle少写了一个'a',手误对程序员来说是经常发生的事,有时能够及时发现纠错,有时就容易忽略. 建议大家将写好的测试无误的保存起来, ...

  5. 自定义View Measure过程(2)

    目录 目录 1. 作用 测量View的宽/高 在某些情况下,需要多次测量(measure)才能确定View最终的宽/高: 在这种情况下measure过程后得到的宽/高可能是不准确的: 建议在layou ...

  6. Socket学习进阶之基础通信

    服务端代码: using System; using System.Text; using System.Net; using System.Net.Sockets; public class ser ...

  7. openGL深度缓冲区问题

    http://zhidao.baidu.com/question/368299839.html&__bd_tkn__=6aa9196c746cd3357f1eec74aeb127b395029 ...

  8. Altium Designer 总线式布线

    1.常规布线:不详细说了,是个人就知道怎么弄.需要说明的是在布线过程中,可按小键盘的*键或大键盘的数字2键添加一个过孔:按L键可以切换布线层:按数字3可设定最小线宽.典型线宽.最大线宽的值进行切换. ...

  9. springMVC多图片压缩上传的实现

    首先需要在配置文件中添加配置: <!--配置文件的视图解析器,用于文件上传,其中ID是固定的:multipartResolver--> <bean id="multipar ...

  10. Reporting Services的简单使用

    最近公司的功能需要使用报表,用的是微软自带的报表,谈一谈我们的做法,希望可以给想学习的人一些指导 1:新建報表所需的數據源DataSet.cs using System; using System.C ...