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 ...
随机推荐
- MySQL大事务导致的Insert慢的案例分析
[问题] 有台MySQL服务器不定时的会出现并发线程的告警,从记录信息来看,有大量insert的慢查询,执行几十秒,等待flushing log,状态query end [初步分析] 从等待资源来看, ...
- Java中面向对象的理解
按照惯例,先做一个简单的介绍,现在开始学习 Thinging in Java 4 ,一边看,一边记录,我都不想给自己设定时间安排了,毕竟很少实现过.所以就这样吧!不定期的更新,我都会放到博客中的. 所 ...
- ssm框架常见问题
搭建SSM框架时,总是遇到这样那样的问题,有的一眼就能看出来,有的需要经验的积累.现将自己搭建SSM框架时遇到的典型问题总结如下: 一.Struts2框架下的action中无法使用@Autowired ...
- POJ 2778 DNA Sequence(AC自动机+矩阵)
[题目链接] http://poj.org/problem?id=2778 [题目大意] 给出一些字符串,求不包含这些字符串的长度为n的字符串的数量 [题解] 我们将所有串插入自动机计算match,对 ...
- python编程之socket编程基础
python socket编程,首先需要import socket模块 首先创建一个socket对象 expl = socket.socket(socket.AF_INET,socket.SOCK ...
- MyBatis -- generator 逆向工程
一.引言 官网文档:http://www.mybatis.org/generator/index.html 通过使用官方提供的mapper自动生成工具,mybatis-generator-core-1 ...
- HDU 1698 just a hook 线段树,区间定值,求和
Just a Hook Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=1 ...
- MySQL命令行工具各功能说明(转)
MySQL 服务器端使用工具程序 mysqld - SQL 后台程序(即 MySQL 服务器进程).该程序必须启动运行,才能连接服务器来访问数据库. mysqld_safe - 服务器启动脚本,可以通 ...
- MySQL增强版命令行客户端连接工具(mycli)
效果: 安装: http://www.mycli.net/install 官网: http://www.mycli.net/install
- Android File Hierarchy : System Structure Architecture Layout
Most of the Android user are using their Android phone just for calls, SMS, browsing and basic apps, ...