题目:

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. A公司 推荐算法大赛 总结

    一.介绍 ♦通过用户前四个月(04.15~08.15)的用户行为预测用户第五个月(08.15~09.15)将会购买的品牌.用户共有四种行为(type)分别是:点击(0).购买(1).购物车(2).收藏 ...

  2. [bzoj3270] 博物馆 [期望+高斯消元]

    题面 传送门 思路 本题的点数很少,只有20个 考虑用二元组$S=(u,v)$表示甲在$u$点,乙在$v$点的状态 那么可以用$f(S)$表示状态$S$出现的概率 不同的$f$之间的转移就是通过边 转 ...

  3. 【BZOJ 4057 Kingdoms】

    Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 450  Solved: 187[Submit][Status][Discuss] Descriptio ...

  4. 【马克-to-win】—— 学习笔记

    声明 以下学习内容转载自:http://www.mark-to-win.com/ 社区,由马克java社区创始人---"马克-to-win"一人全部独立写作,创作和制作. 非常感谢 ...

  5. java中的Timer

    一个java中用Timer做的简单定时器小程序. package com.test.lx; import java.util.TimerTask; public class TimeTaskTest ...

  6. Reinstall msdtc on Windows

    Reinstall MSDTC The system reported an unexpected error condition. You can resolve this condition by ...

  7. HDU1536&&POJ2960 S-Nim(SG函数博弈)

    S-Nim Time Limit: 2000MS   Memory Limit: 65536KB   64bit IO Format: %I64d & %I64u Submit Status ...

  8. 使用select2插件并添加拼音首字母检索

    项目中要使用下拉检索的时候要支持拼音首字母.本来拼音可以写后台,这里放前台了. 放代码 1. pinyin.js ,最后为了使用方便,直接为string对象添加了扩展方法 /* File Create ...

  9. QML与C++混合编程详解

    1.QML与C++为什么要混合编程 QML与C++为什么要混合编程,简单来说,就是使用QML高效便捷地构建UI,而C++则用来实现业务逻辑和复杂算法,下面介绍了两者间交互的方法与技巧. 2.QML访问 ...

  10. AUTOIT3设置用户包含目录