合并两个有序数组,放在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. 终结者:具体解释Nginx(一)

            相信非常多人都听过Nginx.这个小巧的东西能够和Apache及IIS相媲美. 那么它有什么作用呢?一句话.它是一个减轻Web应用server(如Tomcat)压力和实现Web应用se ...

  2. Spark1.0.0 学习路径

          2014-05-30 Spark1.0.0 Relaease 经过11次RC后最终公布.尽管还有不少bug,还是非常令人振奋. 作为一个骨灰级的老IT,经过非常成一段时间的消沉,再次被点燃 ...

  3. Testin一日游实验室发布的行级APP质量报告:在那里拍携程双赢

    Testin实验室公布国庆出行旅途类APP质量报告:携程力压去哪儿夺冠 2014/09/28 · Testin · 实验室报告 一年一度的十一黄金周即将临近,旅游软件成为每外出行人手机必装软件.为此全 ...

  4. Dp_F Pku1157

    <span style="color:#000099;">/* F - 简单dp Time Limit:1000MS Memory Limit:10000KB 64bi ...

  5. 最新Oracle 和 mysql 的对比参照----开发篇(转)

    Oracle mysql 对比版本 Release 10.2.0.1.0 XE  windowsXP 5.0.45-community-nt-log MySQL Community Edition ( ...

  6. 自定义View视图

    自定义View视图文件查找逻辑 之前MVC5和之前的版本中,我们要想对View文件的路径进行控制的话,则必须要对IViewEngine接口的FindPartialView或FindView方法进行重写 ...

  7. 文《左右c++与java中国的垃圾问题的分析与解决》一bug分析

    文<左右c++与java中国的垃圾问题的分析与解决>一bug分析 DionysosLai(906391500@qq.com) 2014/10/21 在前几篇一博客<关于c++与jav ...

  8. jquery密码强度检测

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  9. 【Head First Javascript】学习笔记0——自己制作chm参考手册素材

    变量声明:var 常量声明:const 数据格式转换: 1.转换函数 parseInt(A):把字符串A转换成整数:其中A为只包含数字的字符串 parseFloat(A):把字符串A转换成浮点数:其中 ...

  10. CSharp设计模式读书笔记(21):状态模式(学习难度:★★★☆☆,使用频率:★★★☆☆)

    模式角色与结构: 示例代码:(本示例在具体状态类中实现状态切换,也可以在环境类中实现状态切换.状态模式一定程度上违背开闭原则) using System; using System.Collectio ...