Merge Sorted Array——LeetCode
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的更多相关文章
- 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 ...
- 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 ...
- [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 ...
- [LeetCode] 88. Merge Sorted Array 混合插入有序数组
Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 as one sorted array. Note: T ...
- 【LeetCode练习题】Merge Sorted Array
Merge Sorted Array Given two sorted integer arrays A and B, merge B into A as one sorted array. Note ...
- Leetcode#88. Merge Sorted Array(合并两个有序数组)
题目描述 给定两个有序整数数组 nums1 和 nums2,将 nums2 合并到 nums1 中,使得 num1 成为一个有序数组. 说明: 初始化 nums1 和 nums2 的元素数量分别为 m ...
- 【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 ...
- LeetCode练题——88. Merge Sorted Array
1.题目 88. Merge Sorted Array——Easy Given two sorted integer arrays nums1 and nums2, merge nums2 into ...
- 43. Merge Sorted Array && LRU Cache
Merge Sorted Array OJ: https://oj.leetcode.com/problems/merge-sorted-array/ Given two sorted integer ...
随机推荐
- Android手机适配——UI图片适配
转载请注明出处:http://blog.csdn.net/guolin_blog/article/details/50727753 在Android项目当中,drawable文件夹都是用来放置图片资源 ...
- Android客户端与服务端交互之登陆示例
Android客户端与服务端交互之登陆示例 今天了解了一下android客户端与服务端是怎样交互的,发现其实跟web有点类似吧,然后网上找了大神的登陆示例,是基于IntentService的 1.后台 ...
- C#学习第二天
在C#中数据类型大概有两类:值类型和引用类型,需要由定义类型的开发人员决定在什么地方分配一个实例. 值类型和引用类型在使用原理上也有所不同,值类型在使用时是传递或者得到一个值的副本,而引用类型在使用时 ...
- 排名最重要的三个优化阶段分析 --------------------->>转至(卧牛SEO/武汉SEO http://blog.sina.com.cn/zhengkangseo )
网站排名,不是一两天能够决定的.要想取得好的排名,得分时间分阶段地做排名,网站优化分前期,中期,后期,怎么来区别不同的阶段该用怎样的优化手段.今晚SEO研究中心创始人Moon老师分享:排名最重要的三个 ...
- Android中Action
1 Intent.ACTION_MAIN String: android.intent.action.MAIN 标识Activity为一个程序的开始.比较常用. <activity androi ...
- 二、T4模板
上文带大家见识了下T4,这里呢开始介绍T4模板有关的内容.关于T4模板介绍和使用网上一搜一箩筐,想深入研究的可以自行去找些资料,这里只介绍接下来我们需要使用的一些知识,不会面面俱到的讲T4模板所有的知 ...
- Sqlserver2005手动备份远程数据库到本地数据库方法
1,在本地数据库中新建一个数据库名,如local 选中local,鼠标右键,任务,导入数据 2下一步: 注意:服务器名称写远程连接的主机的IP, 数据库选中你要备份的远程数据库. 3下一步: 注意:服 ...
- 再谈CMake与RPATH
之前写过一篇<探讨CMake中关于RPATH的使用>,只要针对的方面是在编译生成之后(不包括安装的make install)如何去除RPATH的问题.今天给大家主要介绍一下如何让CMake ...
- jvectormap 中国地图 (包括香港、台湾、澳门)
一个完整的中国地图(各个省,市.还有国两制),谢谢大家. 忘了网上哪位的范例,我加了些修改. <html xmlns="http://www.w3.org/1999/xhtml&quo ...
- [转]JavaScript函数和数组总结
转自:http://www.uml.org.cn/AJAX/201307264.asp 写的不错,对我有很多帮助,最近准备全面的学习研究一下ES5,先转载一下这篇文章. JavaScript函数 1. ...