题目:

合并排序数组 II

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

样例

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

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

注意

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

解题:

这里给的是两个数组,上题给的是ArrayList格式,比较好处理,重新定义一个长度m+n的数组,但是A的数组长度是m+n,可以从后面将元素插入的A数组中

class Solution {
/**
* @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
*/
public void mergeSortedArray(int[] A, int m, int[] B, int n) {
// write your code here
int k = m + n -1;
m--;
n--;
while(m>=0 && n>=0){
if(A[m] >= B[n]){
A[k--] = A[m--];
}else{
A[k--] = B[n--];
}
}
while(n>=0){
A[k--] = B[n--];
} } }

也可以这样写

class Solution {
/**
* @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
*/
public void mergeSortedArray(int[] A, int m, int[] B, int n) {
// write your code here
int k = m + n -1;
m--;
n--;
while(k>=0){
if(n<0 || (m>=0 && A[m] >= B[n])){
A[k--] = A[m--];
}else{
A[k--] = B[n--];
}
}
} }

Python程序:

class Solution:
"""
@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
"""
def mergeSortedArray(self, A, m, B, n):
# write your code here
for i in range(n):
A[m+i] = B[i]
A.sort()
return A

总耗时: 233 ms

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

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

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

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

  3. LintCode之合并排序数组II

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

  4. 6. 合并排序数组 II

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

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

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

  6. [LintCode] 合并排序数组

    A subroutine of merge sort. class Solution { public: /** * @param A and B: sorted integer array A an ...

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

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

  8. lintcode:合并排序数组

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

  9. lintcode-64-合并排序数组 II

    64-合并排序数组 II 合并两个排序的整数数组A和B变成一个新的数组. 注意事项 你可以假设A具有足够的空间(A数组的大小大于或等于m+n)去添加B中的元素. 样例 给出 A = [1, 2, 3, ...

随机推荐

  1. JQGrid+Webservice+LINQ

    <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="jqgridtest.asp ...

  2. Tomcat部署web应用的方式

    对Tomcat部署web应用的方式总结,常见的有以下四种: 1.[使用控制台部署] 访问Http://localhost:8080,并通过Tomcat Manager登录,进入部署界面即可. 2.[利 ...

  3. php生成txt文件换行问题

    用双引号即"\r\n"换行,不能用单引号即'\r\n'.

  4. openerp 经典收藏 workflow中的‘非典型’自动触发器trigger_model(转载)

    workflow中的‘非典型’自动触发器trigger_model 原文:http://cn.openerp.cn/workflow%E4%B8%AD%E7%9A%84%E9%9D%9E%E5%85% ...

  5. django post分号引发的问题

    利用jquery的ajax传值 $.ajax({ type:"POST", url:"", data:"content"=content, ...

  6. Oracle 10g下载链接

    用迅雷下载: http://download.oracle.com/otn/linux/oracle10g/10201/10201_database_linux_x86_64.cpio.gz http ...

  7. 键盘样式风格有关设置-iOS开发

    一.键盘风格 UIKit框架支持8种风格键盘. typedef  enum  { UIKeyboardTypeDefault,                 // 默认键盘:支持所有字符 UIKey ...

  8. 【python】网络爬虫抓取图片

    利用python抓取网络图片的步骤: 1.根据给定的网址获取网页源代码 2.利用正则表达式把源代码中的图片地址过滤出来 3.根据过滤出来的图片地址下载网络图片 今天我们用http://www.umei ...

  9. sql Mirroring

    http://www.codeproject.com/Articles/109236/Mirroring-a-SQL-Server-Database-is-not-as-hard-as http:// ...

  10. SAP如何使用关于序列号的表