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 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 and n respectively.
题解:
这道题是说让B merge到 A 里面。
先复习下原本我们在MergeSort里面怎么利用一个新建的数量来merge two array:
代码如下:
1 public int[] mergeTwoList(int[] A, int[] B) {
2 int[] C = new int[A.length + B.length];
3 int k = 0;
4 int i = 0;
5 int j = 0;
6 while(i < A.length && j < B.length) {
7 if (A[i] < B[j])
8 C[k++] = A[i++];
9 else
C[k++] = B[j++];
}
while (i < A.length)
C[k++] = A[i++];
while (j < B.length)
C[k++] = B[j++];
return C;
}
然后我们再顺便复习下,怎么merge two linked list,代码如下:
1 public ListNode mergeTwoLists(ListNode leftlist, ListNode rightlist){
2 if(rightlist == null)
3 return leftlist;
4 if(leftlist == null)
5 return rightlist;
6
7 ListNode fakehead = new ListNode(-1);
8 ListNode ptr = fakehead;
9 while(rightlist!=null&&leftlist!=null){
if(rightlist.val<leftlist.val){
ptr.next = rightlist;
ptr = ptr.next;
rightlist = rightlist.next;
}else{
ptr.next = leftlist;
ptr = ptr.next;
leftlist = leftlist.next;
}
}
if(rightlist!=null)
ptr.next = rightlist;
if(leftlist!=null)
ptr.next = leftlist;
return fakehead.next;
}
可以看出merge的思路都是在从头比较两个list的value,用两个指针分别指向当前要比较的node上面。而且最后都会处理下剩下的元素。
而这道题是不能借助一个新的array的,那么我们就不好从前往后比了(不好插入位置)。方便的方法是从后往前比,然后最后处理剩下的元素。
代码如下:
public void merge(int A[], int m, int B[], int n) {
while(m > 0 && n > 0){
if(A[m-1] > B[n-1]){
A[m+n-1] = A[m-1];
m--;
}else{
A[m+n-1] = B[n-1];
n--;
}
}
while(n > 0){
A[m+n-1] = B[n-1];
n--;
}
}
Merge Sorted Array leetcode java(回顾MergeTwoArray和MergeTwoLinkedList)的更多相关文章
- 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]
Given two sorted integer arrays A and B, merge B into A as one sorted array. Note:You may assume tha ...
- Find Minimum in Rotated Sorted Array leetcode java
题目: Suppose a sorted array is rotated at some pivot unknown to you beforehand. (i.e., 0 1 2 4 5 6 7 ...
- Remove Duplicates From Sorted Array leetcode java
算法描述: Given a sorted array, remove the duplicates in place such that each element appear only once a ...
- Search in Rotated Sorted Array leetcode java
题目: Suppose a sorted array is rotated at some pivot unknown to you beforehand. (i.e., 0 1 2 4 5 6 7 ...
- Median of Two Sorted Array leetcode java
题目: There are two sorted arrays A and B of size m and n respectively. Find the median of the two sor ...
- LeetCode算法题-Merge Sorted Array(Java实现)
这是悦乐书的第161次更新,第163篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第20题(顺位题号是88).给定两个排序的整数数组nums1和nums2,将nums2中 ...
- [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 ...
随机推荐
- c#/asp.net实现炫酷仿调色板/颜色选择器功能
asp.net 之颜色选择器,仿调色板功能 1. 插件非常容易使用,只需引用相应的js文件和css样式文件即可,见代码示例,插件精小,炫酷 2. 只需要初始化即可使用,并且选择的颜色会在文本框中以16 ...
- 网络数据修改工具netsed
网络数据修改工具netsed 通过修改网络数据,可以绕过软件和防火墙的限制,达到特定的目的.Kali Linux提供一个简易数据修改工具netsed.该工具支持对TCP和UDP的数据进行修改.渗透 ...
- 【Performance】chrome调试面板
本篇文章以chrome版本67.0.3396.99为例,说明性能方面的调试.
- 【WIN10】VisualStateManager使用說明
Demo下載:http://yunpan.cn/cFjgPtWRHKH9H 访问密码 c4b7 顧名思義,視圖狀態管理器. 在WPF中,它的功能似乎更強大.在UWP中,閹割了GotElementSt ...
- BZOJ 3963 HDU3842 [WF2011]MachineWorks cdq分治 斜率优化 dp
http://acm.hdu.edu.cn/showproblem.php?pid=3842 写的check函数里写的<但是应该是<=,调了一下午,我是个zz. 就是普通的斜率优化因为有两 ...
- Java Maven:spring boot + Mybatis连接MySQL,通用mapper的增删改查,映射实现多表查询
1. MySQL自带库test添加表user.role 角色表role 用户表user 2. 添加依赖,配置属性 相关依赖:百度即可,此处略 application.properties spring ...
- Jenkins 使用 maven 出现C:\Windows\system32\config\systemprofile的解决
jenkins 使用 maven 出现 C:\Windows\system32\config\systemprofile 的原因是 Jenkins 服务启动的账号使用了系统的账号,在服务里改成具体的桌 ...
- Spotlight on linux 监控 linux服务器资源
步骤一:在window主机上安装spotlight 下载地址:http://worlddownloads.quest.com.edgesuite.net/Repository/www.quest.co ...
- spring @Transactional注解无效
<!-- 配置事务管理器 --> <bean id="transactionManager" class="org.springframework.jd ...
- VPP电源控制(VPP Power)-- 由DC-DC变换集成电路MC34063组成
http://www.willar.com/article/article_view.asp?id=463 由DC-DC变换集成电路MC34063组成,34063 广泛应用于于DC-DC的电源转换电路 ...