合并两个有序数组,放在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. [SignalR]注册路由

    原文:[SignalR]注册路由 1.注册SignalR路由 在Asp.Net中,若是SignalR 1.*版本,在Global.asax文件中定义如下: 在Asp.Net中,若是SignalR 2. ...

  2. CPU 风扇清理灰尘加油全过程图解

    主机电源风扇因为使用时间长,风扇轴承的润滑油耗尽,导致风扇转速下降或是不转,引起电源热量无法有效排除而造成电脑常常死机,解决的方法有几种. 现图讲解明最简单省钱的办法例如以下: 1.把电源从主机上拆下 ...

  3. Preemption Context Switches 和 Synchronization Context Switches

    Preemption Context Switches测量操作系统任务调度线程处理器上执行的次数,以及切换到较高-priority螺纹,数. Synchronization context switc ...

  4. (大数据工程师学习路径)第三步 Git Community Book----Git基本用法(上)

    一.git的初始化 1.Git 配置 使用Git的第一件事就是设置你的名字和email,这些就是你在提交commit时的签名. $ git config --global user.name &quo ...

  5. uva 10228 - Star not a Tree?(模拟退火)

    题目链接:uva 10228 - Star not a Tree? 题目大意:给定若干个点,求费马点(距离全部点的距离和最小的点) 解题思路:模拟退火算法,每次向周围尝试性的移动步长,假设发现更长处, ...

  6. 【Android】九宫格实现

    第一步,布局文件 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns ...

  7. UVALive 4730 Kingdom +段树和支票托收

    主题链接:点击打开链接 题意见白书P248 思路: 先把读入的y值都扩大2倍变成整数 然后离散化一下 用线段树来维护y轴 区间上每一个点的 城市数量和联通块数量. 然后用并查集维护每一个联通块及联通块 ...

  8. WP 前台或后台显示ShellToast

    原文:WP 前台或后台显示ShellToast using Microsoft.Phone.Shell; ShellToast toast = new ShellToast(); toast.Titl ...

  9. Android中的“再按一次返回键退出程序”实现[转]

    用户退出应用前给出一个提示是很有必要的,因为可能是用户并不真的想退出,而只是一不小心按下了返回键,大部分应用的做法是在应用退出去前给出一个Dialog,我觉得这样不太友好,用户还得移动手指去按dial ...

  10. DFS-hdu-2821-Pusher

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=2821 题目意思: 给一个n*n的矩阵,里面有些位置是空的,有些位置有箱子(a代表一个箱子,b代表两个 ...