合并两个有序数组,放在A中,A中的空间足够。

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 andn respectively.

从后往前放,比较A和B的从后往前的,比较大的放在m+n-1的位置。然后一直减减。

class Solution {
public:
void merge(int A[], int m, int B[], int n)
{while(m > && n > )
{
if (A[m-] > B[n-])
{
A[m+n-] = A[m-];
m--;
}
else
{
A[m+n-] = B[n-];
n--;
}
}
if (m == )
while(n > )
{
A[n-] = B[n-];
n--;
}
return ;
}
};

也可以写短一点:

class Solution {
public:
void merge(int A[], int m, int B[], int n)
{
int nowA = m - , nowB = n - , last = n + m - ;
while(nowA >= && nowB >=)
{
A[last--] = A[nowA] > B[nowB] ? A[nowA--] : B[nowB--];
}
while(nowB >= )
A[last--] = B[nowB--];
return ;
}
};

2015/03/31:

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--];
}
while(i >= ){
A[k--] = A[i--];  // 多余了
}
while(j >= ){
A[k--] = B[j--];
}
}
};

原来多余的写了一部分  A的不用赋值给A可以,因为就是A啊

python:

class Solution:
# @param A a list of integers
# @param m an integer, length of A
# @param B a list of integers
# @param n an integer, length of B
# @return nothing(void)
def merge(self, A, m, B, n):
i, j, k= m -, n -, m + n -
while i >= and j >=:
if A[i] > B[j]:
A[k] = A[i]
i -=
else:
A[k] = B[j]
j -=
k -=
while j >= :
A[k] = B[j]
k -=
j -=

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

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

    Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 as one sorted array. Note: T ...

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

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

  3. LeetCode 88. Merge Sorted Array(合并有序数组)

    Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 as one sorted array. Note:Yo ...

  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

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

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

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

  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(easy)

    Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 as one sorted array. Note:Yo ...

随机推荐

  1. quick-cocos2d-x游戏开发【5】——创建菜单

    一个菜单是游戏中的一个基本要素,quick在里面menuItem有两个包.一个是图片菜单.一个文本菜单. 一个.图片菜单ui.newImageMenuItem(params) 參数: image: 正 ...

  2. Oracle to_char,to_date

    一.在oracle中,当想把字符串为‘2011-09-20 08:30:45’的格式转化为日期格式,我们可以使用oracle提供的to_date函数. sql语句为: SELECT to_date(' ...

  3. ENode简介与各种资源汇总

    ENode简介与各种资源汇总 ENode是什么 ENode是一个.NET平台开源的应用开发框架,为开发人员提供了一套完整的基于DDD+CQRS+ES+(in-memory)+EDA架构风格的解决方案. ...

  4. 添加AD验证(域身份验证)到现有网站

    每个网站几乎都会有用户登录的模块,登录就会涉及到身份验证的过程.通常的做法是在页面上有个登录的Form,然后根据用户名和密码到数据库中去进行验证. 而验证后如何在网站的各个页面维持这种认证过的状态,有 ...

  5. 框架搭建资源 (一) V(视图)C(控制)模式

    pom.xml <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncodin ...

  6. SQL Server Log文件对磁盘的写操作大小是多少

    原文:SQL Server Log文件对磁盘的写操作大小是多少 SQL Server 数据库有三种文件类型,分别是数据文件.次要数据文件和日志文件,其中日志文件包含着用于恢复数据库的所有日志信息,SQ ...

  7. Hibernate_10_继承的例子_单表

     只是建一个表,所有属性都包括在此表.使用discriminator 到父和子类之间的区别. 1)父类(Article): public class Article { private Integer ...

  8. MVC 接受Flash上传图片

    /// <summary> /// 经Flash上传图片 /// </summary> /// <param name="uid"></p ...

  9. .net EF 事物 订单流水号的生成 (二):观察者模式、事物、EF

    针对.net EF 事物 订单流水号的生成 (一)  的封装. 数据依然不变. using System; using System.Linq; using System.Transactions; ...

  10. $.each()方法详解

    $.each()方法详解 each()函数具有十分强大的遍历功能,可以遍历一维数组.多维数组.Dom.Json等. 在JavaScript中使用$.each可以大大减轻我们的工作量. 1.处理一维数组 ...