合并两个有序数组,放在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. DataGridView突出

    再看视频的时候,看到视频上面有对DataGrid中的数据进行高亮显示实现功能.当中涉及到一个事件,是DataGrid1_ItemDataBound. 实现的代码例如以下: b.IJ'I.脚e比ontr ...

  2. ASP.NET MVC 3: Razor中的@:和语法

    原文 ASP.NET MVC 3: Razor中的@:和语法 [原文发表地址] ASP.NET MVC 3: Razor’s @: and <text> syntax[原文发表时间] De ...

  3. 添加和删除行的能力table(能够编辑的表的内容)

    页面文件 <html> <head> <meta http-equiv="Content-Type" content="text/html; ...

  4. 使用 CodeIgniter 框架快速开发 PHP 应用(六)

    原文:使用 CodeIgniter 框架快速开发 PHP 应用(六) 简化使用 Session 和安全理论说得够多了! 现在让我们开始写我们自己的应用. 在这一章里,我们将会大致描述一下我们要建立的一 ...

  5. nginx.conf 完整的集群配置

    ###############################nginx.conf 整配置############################### #user nobody; # user 主模 ...

  6. HDU 4946 Area of Mushroom 凸包 第八次多校

    题目链接:hdu 4946 题意:一大神有N个学生,各个都是小神,大神有个二次元空间,每一个小神都有一个初始坐标,如今大神把这些空间分给徒弟们,规则是假设这个地方有一个人比谁都先到这,那么这个地方就是 ...

  7. hightmaps 按地图上显示的统计数据

    离extjs 至 easyui 到html5到hightchars 再到hightmaps.Exjts和easyui很相似,extjs是重量级的,easyui轻量级的.比extjs容易上手.照着dem ...

  8. hadoop编程小技巧(5)---自己定义输入文件格式类InputFormat

    Hadoop代码測试环境:Hadoop2.4 应用:在对数据须要进行一定条件的过滤和简单处理的时候能够使用自己定义输入文件格式类. Hadoop内置的输入文件格式类有: 1)FileInputForm ...

  9. Android MediaPlayer 和 NativePlayer 播放格式控制

    对于本机MediaPlayer 支持格型式试验: 对于原生 NativeMedia 的支持格式測试: 这个支持就比較失望了,眼下測试的手机仅仅支持 H.264视频及AAC音频,其他的格式都不支持. 使 ...

  10. POJ1719- Shooting Contest(二分图最大匹配)

    题目链接 题意:给定一个矩阵,每列有两个白点,其它都是黑点,如今要求每列选一个白点,使得每一行至少包括一个白点被选中 思路:利用白点所在的位置用行指向列建图,用行去匹配列,最大匹配数假设不等于行数的话 ...