88. Merge Sorted Array
题目:
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.
链接: http://leetcode.com/problems/merge-sorted-array/
题解: 从后向前比较。 Time Complexity - O(m + n), Space Complexity - O(1)。
public class Solution {
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 --;
}
}
}
Update:
public class Solution {
public void merge(int[] nums1, int m, int[] nums2, int n) {
if(nums1 == null || nums2 == null)
return;
int k = m - 1, l = n - 1;
for(int i = m + n - 1; i >= 0; i--) {
if(k >= 0 && l >= 0)
nums1[i] = (nums1[k] > nums2[l]) ? nums1[k--] : nums2[l--];
else if (k < 0)
nums1[i] = nums2[l--];
else if (l < 0)
return;
}
}
}
二刷:
Java:
Time Complexity - O(m + n), Space Complexity - O(1)。
public class Solution {
public void merge(int[] nums1, int m, int[] nums2, int n) {
if (nums1 == null || nums2 == null) {
return;
}
while (m > 0 || n > 0) {
if (m > 0 && n > 0) {
nums1[m + n - 1] = nums1[m - 1] > nums2[n - 1] ? nums1[m-- - 1] : nums2[n-- - 1];
} else if (n > 0) {
nums1[n - 1] = nums2[n-- - 1];
} else {
break;
}
}
}
}
三刷:
3/18/2016: 这道题在Microsoft onsite第四轮里还真被问到了,赶紧使用二刷的方法秒了。结果最后通过recruiter询问到的feedback是 : coding is rough。翻译过来就是, 写得太糙...... 看来真的不应该用太多的 ++, -- 和tenary operator。所以这一遍老老实实地写。
我们先确定边界条件。然后再m > 0 并且n > 0的情况下对数组nums1进行从后向前地遍历。 当nums1的最后一个元素nums1[m - 1]比nums2的最后一个元素nums2[n - 1]大的时候,nums1[m + n - 1] = nums1[m - 1],并且我们设置m--,比较下一个元素。否则nums[m + n - 1] = nums2[n - 1],n--。 循环结束后,我们判断n是否仍然大于0, 如果是的话,此时m为0, 我们继续更新nums1[n - 1] = nums2[n - 1], n--。
Java:
Time Complexity - O(m + n), Space Complexity - O(1)
public class Solution {
public void merge(int[] nums1, int m, int[] nums2, int n) {
if (nums1 == null || nums2 == null || m < 0 || n < 0) {
return;
}
while(m > 0 && n > 0) {
if (nums1[m - 1] > nums2[n - 1]) {
nums1[m + n - 1] = nums1[m - 1];
m--;
} else {
nums1[m + n - 1] = nums2[n - 1];
n--;
}
}
while (n > 0) {
nums1[n - 1] = nums2[n - 1];
n--;
}
}
}
Update:
public class Solution {
public void merge(int[] nums1, int m, int[] nums2, int n) {
if (nums1 == null || nums2 == null || nums1.length == 0) return;
while (m > 0 && n > 0) {
if (nums1[m - 1] > nums2[n - 1]) {
nums1[m + n - 1] = nums1[m - 1];
m--;
} else {
nums1[m + n - 1] = nums2[n - 1];
n--;
}
}
while (n > 0) {
nums1[n - 1] = nums2[n - 1];
n--;
}
}
}
88. Merge Sorted Array的更多相关文章
- Leetcode#88. Merge Sorted Array(合并两个有序数组)
题目描述 给定两个有序整数数组 nums1 和 nums2,将 nums2 合并到 nums1 中,使得 num1 成为一个有序数组. 说明: 初始化 nums1 和 nums2 的元素数量分别为 m ...
- 88. Merge Sorted Array【easy】
88. Merge Sorted Array[easy] Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 ...
- LeetCode练题——88. Merge Sorted Array
1.题目 88. Merge Sorted Array——Easy Given two sorted integer arrays nums1 and nums2, merge nums2 into ...
- [LeetCode] 88. Merge Sorted Array 混合插入有序数组
Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 as one sorted array. Note: T ...
- 【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
Problem: Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 as one sorted array ...
- 【LeetCode】88 - Merge Sorted Array
Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 as one sorted array. Note:Yo ...
- LeetCode OJ 88. Merge Sorted Array
Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 as one sorted array. Note:Yo ...
- LeetCode 88. Merge Sorted Array(合并有序数组)
Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 as one sorted array. Note:Yo ...
随机推荐
- spring与MyBatis结合
下面将介绍使用spring+mybatis的开发样例: 首先,笔者创建的是一个maven工程,在开发先先导入相关的依赖jar: pom.xml: <dependencies> <de ...
- centos crontab 定时任务详解
安装crontab: yum install crontabs 说明: /sbin/service crond start //启动服务 /sbin/service crond stop //关闭服务 ...
- 微软职位内部推荐-Sr. Dev Lead
微软近期Open的职位: JD 如果你想试试这个职位,请跟我联系,我是微软的员工,可以做内部推荐.发你的中英文简历到我的邮箱:Nicholas.lu.mail(at)gmail.com
- 【转载】link和@import的区别
link和@import的区别 原文地址:http://www.cnblogs.com/zbo/archive/2010/11/17/1879590.html 页面中使用CSS的方式主要有3种:行内添 ...
- js之正则表达式(上)
1.正则表达式的创建方式 两种方式创建:通过new修饰符创建和字面量的方式创建 1>new修饰符方式创建 var b2=new RegExp('Box','ig'); //第二个参数是 模式字符 ...
- php发送get、post请求的几种方法
方法1: 用file_get_contents 以get方式获取内容 <?php $url='http://www.domain.com/'; $html = file_get_contents ...
- 2109&2535: [Noi2010]Plane 航空管制 - BZOJ
Description世博期间,上海的航空客运量大大超过了平时,随之而来的航空管制也频频发生.最近,小X就因为航空管制,连续两次在机场被延误超过了两小时.对此,小X表示很不满意. 在这次来烟台的路上, ...
- 【转载】Ext中关于Ext.QuickTips.init()的使用
免责声明: 本文转自网络文章,转载此文章仅为个人收藏,分享知识,如有侵权,请联系博主进行删除. 原文作者:然嗄 原文地址:http://www.cnblogs.com/jia ...
- IntelliJ IDEA 15 显示工具栏及底部周边工具栏
- PHP防止SQL注入与几种正则表达式讲解
注入漏洞代码和分析 代码如下: <?php function customerror($errno, $errstr, $errfile, $errline) { echo <b& ...