64-合并排序数组 II

合并两个排序的整数数组A和B变成一个新的数组。

注意事项

你可以假设A具有足够的空间(A数组的大小大于或等于m+n)去添加B中的元素。

样例

给出 A = [1, 2, 3, empty, empty], B = [4, 5]

合并之后 A 将变成 [1,2,3,4,5]

标签

数组 排序数组 脸书

思路

从后向前遍历数组

code

class Solution {
public:
/**
* @param A: sorted integer array A which has m elements,
* but size of A is m+n
* @param B: sorted integer array B which has n elements
* @return: void
*/
void mergeSortedArray(int A[], int m, int B[], int n) {
// write your code here
int size = m + n, i = 0, a = m-1, b = n-1;
for(i=size-1; i>=0; i--){
if(A[a] <= B[b]) {
A[i] = B[b];
b--;
}
else {
A[i] = A[a];
a--;
}
}
}
};

lintcode-64-合并排序数组 II的更多相关文章

  1. LintCode之合并排序数组II

    题目描述: 分析:题目的意思是把数组A和数组B合并到数组A中,且数组A有足够的空间容纳A和B的元素,合并后的数组依然是有序的. 我的代码: public class Solution { /* * @ ...

  2. lintcode:合并排序数组 II

    题目: 合并排序数组 II 合并两个排序的整数数组A和B变成一个新的数组. 样例 给出A = [1, 2, 3, empty, empty] B = [4,5] 合并之后A将变成[1,2,3,4,5] ...

  3. lintcode:合并排序数组

    题目: 合并排序数组 合并两个排序的整数数组A和B变成一个新的数组. 样例 给出A=[1,2,3,4],B=[2,4,5,6],返回 [1,2,2,3,4,4,5,6] 挑战 你能否优化你的算法,如果 ...

  4. LintCode——合并排序数组II

    描述:合并两个排序的整数数组A和B变成一个新的数组 样例:给出A=[1,2,3,4],B=[2,4,5,6],返回 [1,2,2,3,4,4,5,6] 1.Python:先将数组B加到数组A之后,然后 ...

  5. [LintCode笔记了解一下]64.合并排序数组

    Given two sorted integer arrays A and B, merge B into A as one sorted array. 思路: 因为A的后面的部分都是空的留出来给我们 ...

  6. [LintCode] 合并排序数组II

    class Solution { public: /** * @param A: sorted integer array A which has m elements, * but size of ...

  7. 64. 合并排序数组.md

    描述 合并两个排序的整数数组A和B变成一个新的数组. 你可以假设A具有足够的空间(A数组的大小大于或等于m+n)去添加B中的元素. 您在真实的面试中是否遇到过这个题? 样例 给出 A = [1, 2, ...

  8. 6. 合并排序数组 II

    6. Merge Two Sorted Arrays Description Merge two given sorted integer array A and B into a new sorte ...

  9. LintCode 6---合并排序数组 II

    import java.util.Arrays; public class Lint6 { /* * 合并两个排序的整数数组A和B变成一个新的数组.新数组也要有序. */ public static ...

  10. [容易]合并排序数组 II

    题目来源:http://www.lintcode.com/zh-cn/problem/merge-sorted-array/

随机推荐

  1. jquery mobile 移动web(6)

    jquery mobile 针对移动端设备的事件类型. 1.touch 事件. tap 快速触摸屏幕并且离开,类似一种完整的点击操作. taphold 触摸屏幕并保持一段时间. swipe 在1秒内水 ...

  2. 使用poi读取excel文件 Cannot get a text value from a numeric cell

    我这样转换得到一个excel文本域的值 Cell cell = row.getCell(c); cell.setCellType(Cell.CELL_TYPE_STRING); String park ...

  3. 【例题收藏】◇例题·6◇ 电压机制(voltage)

    ◆例题·6◆ 电压机制 周六日常模拟赛……已经不知道该说什么了(感觉做不出来的都是好题) ▷ 题目 (终于不用自己翻译英文题了╮(╯-╰)╭) [问题描述] 科学家在“无限神机”(Infinity M ...

  4. 进一步理解 frame 和 bounds

    总结一下 iOS中 frame 和 bounds之间的区别    综述 frame和bounds都是描述一块矩形区域,但是他们是有区别的 frame:可视范围,可以理解为控件的大小,把控件当作边缘很薄 ...

  5. Learning Experience of Big Data:The First Day-Try to set up a network connection on my virtural machine

    After we install our virtual machine,the first thing we should do is to set up a network connection ...

  6. 2.从print到自省

    print是一个函数   为什么print是一个函数呢?可以在交互式解释器下 输入: >>> type(print) 输出: <class 'builtin_function_ ...

  7. Scrapy框架的基本使用

    安装 pip install scrapy 基础使用 1. 创建一个工程:scrapy startproject 2. 在工程目录下创建一个爬虫文件 cd 工程 scrapy genspider 爬虫 ...

  8. 按升序打印X,Y,Z的整数值

    #include <stdio.h> #define TRUE 1 #define FALSE 0 int main() { int x,y,z; printf("x: &quo ...

  9. Test类实验

    package PC_TEST; class CPU{ int speed; CPU(){ speed=0; } CPU(int k){ speed=k; } void setSpeed(int k) ...

  10. Python自动化运维——系统性能信息模块

    Infi-chu: http://www.cnblogs.com/Infi-chu/ 模块:psutil psutil是一个跨平台库,可以很轻松的为我们实现获取系统运行的进程和资源利用率等信息. 功能 ...