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 mand n respectively.

https://leetcode.com/problems/merge-sorted-array/

题意就是给定两个有序数组,把B中的元素合并到A中,可以假定A中有足够的空间。

怎么做才能是时间复杂度最低呢,与合并两个有序链表不同,两个数组的元素是可以通过下标直接访问的,而且要合并两个数组,元素肯定是要移动的,不像链表中改变一下next指针就可以了。最优的方式当时是从后往前遍历,因为合并的数组大小为m+n,那么合并后的数组最后一个元素的下标就是m+n-2,依次比较两个数组最末的元素,把较大的一个放置在合并后的数组的末端。

Talk is cheap>>

首先上清晰版,边界也清晰的处理了:

   public void merge(int A[], int m, int B[], int n) {
int i = 1, j = 1;
if (m == 0) {
System.arraycopy(B,0,A,0,n);
return;
}
if (n == 0)
return;
while (i <= m && j <= n) {
while (i <= m && j <= n && A[m - i] >= B[n - j]) {
A[m + n - i - j + 1] = A[m - i];
i++;
}
while (i <= m && j <= n && A[m - i] < B[n - j]) {
A[m + n - i - j + 1] = B[n - j];
j++;
}
}
if (j <= n) {
System.arraycopy(B, 0, A, 0, m + n - i - j + 2);
}
}

下面是压缩版,把边界条件缩入了while和if的判断中,并且压缩了while主循环,两个方法的执行时间几乎是一样的:

   public void merge(int A[], int m, int B[], int n) {
int i = 1, j = 1;
while (i <= m && j <= n) {
A[m + n - i - j + 1] = A[m - i] > B[n - j] ? A[m - i++] : B[n - j++];
}
if (j <= n) {
System.arraycopy(B, 0, A, 0, m + n - i - j + 2);
}
}

Merge Sorted Array——LeetCode的更多相关文章

  1. Merge Sorted Array [LeetCode]

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

  2. Merge Sorted Array leetcode java(回顾MergeTwoArray和MergeTwoLinkedList)

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

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

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

  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

    Merge Sorted Array Given two sorted integer arrays A and B, merge B into A as one sorted array. Note ...

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

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

  7. 【LeetCode】88. Merge Sorted Array (2 solutions)

    Merge Sorted Array Given two sorted integer arrays A and B, merge B into A as one sorted array. Note ...

  8. LeetCode练题——88. Merge Sorted Array

    1.题目 88. Merge Sorted Array——Easy Given two sorted integer arrays nums1 and nums2, merge nums2 into  ...

  9. 43. Merge Sorted Array && LRU Cache

    Merge Sorted Array OJ: https://oj.leetcode.com/problems/merge-sorted-array/ Given two sorted integer ...

随机推荐

  1. 多线程下的performSelector和NSThread的使用

    多线程下的performSelector和NSThread的使用 NSThread的多线程使用: 我们可以使用这两种方法来使用线程中的问题 - (id)initWithTarget:(id)targe ...

  2. 异步编程中使用帮助类来实现Thread.Start()的示例

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...

  3. 连接管理VMware SphereESXi

    连接管理VMware SphereESXi 1. 准备 下载VMware-viclient-all-5.5.0-1993072,并按照提示安装 2. 使用VMware Sphere Client链接事 ...

  4. .net(全局文件,错误页,静态页,IIS配置及防黑)

    添加全局应用程序类. <%@ Application Language="C#" %> <script runat="server"> ...

  5. codevs1955光纤通信(并查集)

    /* 第一眼以为就是个区间覆盖 然后敲完提交60分0.0 然而觉得自己的做法很对 以为数据错了 后来发现XXX他的牛棚是一圈(牛过得挺好的啊 还能赏湖...) 然后枚举断开的点 可惜n=750 p=1 ...

  6. INSTALL_PARSE_FAILED_MANIFEST_MALFORMED 错误

    在eclipse编译好文件之后,往AVD中安装apk,报错如下:INSTALL_PARSE_FAILED_MANIFEST_MALFORMED一般来说只需要检查AndroidManifest.xml中 ...

  7. 解决Windows8前面板耳机无声的问题

    Windows8已经到来不久了,相信很多朋友已经在使用,在使用时也许会遇到前面板耳机无声的问题,网上的其他办法很麻烦还不一定能解决,在这里我会给大家提供最简单的办法解决这个问题. 百度经验:jingy ...

  8. Android ScrollView 不能滚动但是有滚动条

    如果一切都检查完毕,没有任何设置为不能滚动,而且outouch事件也没有被拦截的话,那么在布局文件中检查下是否在ScrollView中的子View中设置了margin_top属性.如果设置了,那么Sc ...

  9. Xcode7 通过 Single View Application 得到一个 Empty Application 工程

    方法: 创建一个 Empty Application 工程 下面还是详细的说一下通过一个 Single View Application 工程得到一个 Empty Application 工程的方法: ...

  10. WPF实现摄像头镜像翻转

    之前的项目需要镜像翻转摄像头视频,使用Aforge.Net来处理视频. 一开始考虑直接从Aforge.Net读取没一帧视频图片,然后复制给WPF的Image控件,这种方法基本很卡,所以放弃了. 考虑到 ...