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.

Summary: It's easy, but should be careful about corner cases, like m is 0, n is 0.

     void merge(int A[], int m, int B[], int n) {
if(n == )
return;
if(m == ) {
memcpy(A, B, n * sizeof(B[]));
return;
}
int a = ;
int b = ;
int a_idx = ;
int new_len = m;
while(true) {
if(A[a] >= B[b]) {
//insert B[b] before A[a]
for(int i = new_len; i >= a + ; i --)
A[i] = A[i - ];
A[a] = B[b];
new_len ++;
b ++;
if(b >= n)
break;
a ++;
}else {
if(a_idx >= m) {
//copy rest B to tail of A
memcpy(A + a, B + b, (n-b)*sizeof(A[]));
break;
}
a ++;
a_idx ++;
}
}
}

Merge Sorted Array [LeetCode]的更多相关文章

  1. Merge Sorted Array——LeetCode

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

  2. Merge Sorted Array leetcode java(回顾MergeTwoArray和MergeTwoLinkedList)

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

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

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

  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练习题】Merge Sorted Array

    Merge Sorted Array Given two sorted integer arrays A and B, merge B into A as one sorted array. Note ...

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

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

  7. 【LeetCode】88. Merge Sorted Array (2 solutions)

    Merge Sorted Array Given two sorted integer arrays A and B, merge B into A as one sorted array. Note ...

  8. LeetCode练题——88. Merge Sorted Array

    1.题目 88. Merge Sorted Array——Easy Given two sorted integer arrays nums1 and nums2, merge nums2 into  ...

  9. 43. Merge Sorted Array && LRU Cache

    Merge Sorted Array OJ: https://oj.leetcode.com/problems/merge-sorted-array/ Given two sorted integer ...

随机推荐

  1. 【原文】前端程序员必须知道的高性能Javascript知识

    原文:前端程序员必须知道的高性能Javascript知识 想必大家都知道,JavaScrip是全栈开发语言,浏览器,手机,服务器端都可以看到JS的身影. 本文会分享一些高效的JavaScript的最佳 ...

  2. jquery之event与originalEvent的关系、event事件对象用法浅析

    在jquery中,最终传入事件处理程序的 event 其实已经被 jQuery 做过标准化处理, 其原有的事件对象则被保存于 event 对象的 originalEvent 属性之中, 每个 even ...

  3. [HDOJ5723]Abandoned country(最小生成树,期望)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5723 题意:求最小生成树,并且求这棵最小生成树上所有边走过次数的期望. 走过次数的期望=Σ边被走过次数 ...

  4. Ubuntu中文输入法的添加

    做了一个英文环境的Ubuntu14.04LTS,为了写博客方便,添加了中文输入法,在网上搜寻了一堆方法,最后找到个靠谱的. 1 添加fcitx输入法框架.(在此框架下有各种输入法) sudo add- ...

  5. 常用的STL查找算法

    常用的STL查找算法 <effective STL>中有句忠告,尽量用算法替代手写循环:查找少不了循环遍历,在这里总结下常用的STL查找算法: 查找有三种,即点线面: 点就是查找目标为单个 ...

  6. Codeforces Round #380 (Div. 2, Rated, Based on Technocup 2017 - Elimination Round 2)C. Road to Cinema 二分

    C. Road to Cinema time limit per test 1 second memory limit per test 256 megabytes input standard in ...

  7. Python基础学习笔记(六)常用列表操作函数和方法

    参考资料: 1. <Python基础教程> 2. http://www.runoob.com/python/python-lists.html 3. http://www.liaoxuef ...

  8. 2013/7/16 HNU_训练赛4

    CF328B Sheldon and Ice Pieces 题意:给定一个数字序列,问后面的数字元素能够组成最多的组数. 分析:把2和5,6和9看作是一个元素,然后求出一个最小的组数就可以了. #in ...

  9. html一般标签、常用标签、表格

    body的属性: bgcolor               页面背景色 text                    文字颜色 topmargin            上边距 leftmargi ...

  10. placeholder在不同浏览器下的表现及兼容方法(转)

    1.什么是placeholder?    placeholder是html5新增的一个属性,当input或者textarea设置了该属性后,该值的内容将作为灰字提示显示在文本框中,当文本框获得焦点(或 ...