题目描述:

分析:题目的意思是把数组A和数组B合并到数组A中,且数组A有足够的空间容纳A和B的元素,合并后的数组依然是有序的。

我的代码:

 public class Solution {
/*
* @param A: sorted integer array A which has m elements, but size of A is m+n
* @param m: An integer
* @param B: sorted integer array B which has n elements
* @param n: An integer
* @return: nothing
*/
public void mergeSortedArray(int[] A, int m, int[] B, int n) {
// write your code here
//创建数组c
int[] c = new int[m+n];
int i=0,j=0,k=0; while(i<m && j<n) {
if(A[i] <= B[j]) {
c[k++] = A[i];
i++;
}else {
c[k++] = B[j];
j++;
}
}
while(i < m) {
c[k++] = A[i];
i++;
}
while(j < n) {
c[k++] = B[j];
j++;
} //将数组c中的值赋给A
for(int r=0; r<c.length; r++) {
A[r] = c[r];
}
}
}

LintCode之合并排序数组II的更多相关文章

  1. lintcode:合并排序数组 II

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

  2. lintcode:合并排序数组

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

  3. 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之后,然后 ...

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

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

  5. 6. 合并排序数组 II

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

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

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

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

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

  8. LintCode之合并排序数组

    题目描述: 我的代码: public class Solution { /* * @param A: sorted integer array A * @param B: sorted integer ...

  9. lintcode 中等题:搜索旋转排序数组II

    题目 搜索旋转排序数组 II 跟进“搜索旋转排序数组”,假如有重复元素又将如何? 是否会影响运行时间复杂度? 如何影响? 为何会影响? 写出一个函数判断给定的目标值是否出现在数组中. 样例 给出[3, ...

随机推荐

  1. ceph部署问题解决

    注意:1.ceph-deploy实用程序将输出文件到当前目录.执行ceph-deploy时确保你在这个目录下.2.不要使用sudo调用ceph-deploy,要么以root用户身份运行它,因为它不会发 ...

  2. “希希敬敬对”团队——敏捷冲刺Alpha过程总结

    “希希敬敬对”团队在七天冲刺过程中每一个小组成员都尽力去完成自己的任务.在合作过程中,总算是有一些成果出现,代码功能能够实现. 对此次冲刺有如下优缺点: 优点: 团队人员合作较多,成员都能够积极响应参 ...

  3. python之getopt

    getopt可以分析输入的参数,根据不同的参数输入不同的命令 getopt.getopt( [命令行参数列表], "短选项", "长选项列表" ) getopt ...

  4. HDU-4081.Qinshihuang'sNationalRoadSystem(次小生成树变种)

    Qin Shi Huang's National Road System Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/3 ...

  5. HDU 4285 circuits( 插头dp , k回路 )

    circuits Time Limit: 30000/15000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total ...

  6. C#取模的理解:为什么当a<b,a%b=a?

    一,取模a%b 1,如果a>b,例如10%7=3,这是什么原因呢?可以根据下面的理解 10 =7*1+3,则模就是3 2,如果a<b,例如7%10 = 7,这时怎么得到的呢?根据下面来理解 ...

  7. IOC详解

    Ioc--控制反转详解(转载  http://www.cnblogs.com/qinqinmeiren/archive/2011/04/02/2151697.html) 本文转载与百度知道,简单例子让 ...

  8. python学习笔记(8):

    一.变量和类型 1.Python基本变量类型: 整数 ,浮点数 ,字符串, 布尔值 ,空值 ,函数, 模块, 类型, 自定义类型 2.变量定义 :变量存储在内存中的值.这就意味着在创建变量时会在内存中 ...

  9. Robot Framework 源码阅读 day1 run.py

    robot里面run起来的接口主要有两类 run_cli def run_cli(arguments): """Command line execution entry ...

  10. WEB服务动静结合

    基本介绍 1)WEB服务仅能处理静态请求,如果处理动态请求则需要对应的动态资源服务软件,即:应用程序服务软件 2)常见的应用服务软件有:PHP.Java.Python等 3)问题:WEB服务如何与外部 ...