leetcode笔记:Merge Sorted Array
一.题目描写叙述
二.解题技巧
这道题不存在复杂的分析过程和边界条件。假设单纯得考虑从小到大地将两个数组进行合并的话。每次在num1中插入一个数的话,须要将后面的元素都向后移动一位。这样。整个处理过程的时间复杂度为O(m*n)。
因为两个数组的元素的个数是知道的。同一时候,合并后的数组也是递增排序的,也就是说,排序之后的数组的最大值是放在最后面的。因此,我们能够从后往前遍历,也就是将最大值放在第一个数组的m+n-1位置。然后将次最大值放在m+n-2位置,依次类推。这样在将元素放置到合适位置的时候,就不须要移动元素,这种方法的时间复杂度为O(m+n)。
三.演示样例代码
// 时间复杂度O(m+n),空间复杂度O(1)
class Solution {
public:
void merge(int A[], int m, int B[], int n) {
int ia = m - 1, ib = n - 1, icur = m + n - 1;
while (ia >= 0 && ib >= 0) {
A[icur--] = A[ia] >= B[ib] ?
A[ia--] : B[ib--];
}
while (ib >= 0) {
A[icur--] = B[ib--];
}
}
};
// 使用STL
#include <iostream>
#include <vector>
using std::vector;
class Solution
{
public:
void merge(vector<int>& nums1, int m, vector<int>& nums2, int n)
{
int ResultIndex = m + n - 1;
m--;
n--;
while (m >= 0 || n >= 0)
{
if (m < 0)
{
nums1[ResultIndex--] = nums2[n--];
continue;
}
if (n < 0)
{
nums1[ResultIndex--] = nums1[m--];
continue;
}
if (m >= 0 && n >= 0)
{
if (nums1[m] > nums2[n])
{
nums1[ResultIndex--] = nums1[m--];
continue;
}
else
{
nums1[ResultIndex--] = nums2[n--];
continue;
}
}
}
}
};
leetcode笔记:Merge Sorted Array的更多相关文章
- [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(合并两个有序数组)
题目描述 给定两个有序整数数组 nums1 和 nums2,将 nums2 合并到 nums1 中,使得 num1 成为一个有序数组. 说明: 初始化 nums1 和 nums2 的元素数量分别为 m ...
- LeetCode 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: T ...
- 【leetcode】Merge Sorted Array
题目描述 Given two sorted integer arrays A and B, merge B into A as one sorted array. Note: You may assu ...
- LeetCode 88 Merge Sorted Array
Problem: Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 as one sorted array ...
- 【题解】【数组】【Leetcode】Merge Sorted Array
Given two sorted integer arrays A and B, merge B into A as one sorted array. Note: You may assume th ...
- 【leetcode】Merge Sorted Array(合并两个有序数组到其中一个数组中)
题目: Given two sorted integer arrays A and B, merge B into A as one sorted array. Note: You may assum ...
- leetcode[89] Merge Sorted Array
合并两个有序数组,放在A中,A中的空间足够. Given two sorted integer arrays A and B, merge B into A as one sorted array. ...
- Leetcode 88. Merge Sorted Array(easy)
Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 as one sorted array. Note:Yo ...
随机推荐
- js中的变量提升和函数提升
从上周开始,我所在的学习小组正式开始了angular的学习,angular是全面支持es6的,所以语法上和以前的angular有了很大的不同,比如变量声明时就抛弃了var,而选择了let和const: ...
- CSS变量实用指南及注意事项
近年来,一些动态特性已经开始成为 CSS 语言本身的一部分. CSS变量 – 官方的术语为 "自定义属性" – 已经已经加入规范并且具有很好的浏览器支持,而 CSS mixins ...
- 2015 Multi-University Training Contest 8 hdu 5384 Danganronpa
Danganronpa Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)Tot ...
- POJ——T2421 Constructing Roads
http://poj.org/problem?id=2421 Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 24132 ...
- 初识BeeFramework
由于近期的项目须要,Hybrid开发成为我開始学习的新知识.非常早之前就了解到两个开发框架--BeeFramework 和 Samurai,可是由于本人一直没有闲暇去研究,所以就一直搁置一旁了.近期才 ...
- node.js mongodb ReplSet
随着web2.0兴起,高并发大数据量的应用对数据库高速响应的性能要求日趋明显,传统的关系型数据库在这方面显得有些乏力.有矛自有盾,内存DB的出现弥补了传统关系型db的不足.眼下市面流行的内存db主要有 ...
- [jzoj 5930] [NOIP2018模拟10.26】山花 解题报告 (质因数分类)
题目链接: http://172.16.0.132/senior/#contest/show/2538/2 题目: 小S决定从某一个节点$u$开始对其子树中与$u$距离小于$K$的节点代表的花树进行采 ...
- vue 初始化项目模板报错
E:\xiaogezi.cn\vue>vue init webpack myProject vue-cli · Failed to download repo vuejs-templates/w ...
- R和中心度、中心势
最近用R画论文里的弦图,恰好借的书里着重写了中心度等问题. 网上流行一套密歇根大学社交计算的教程.但是前两年看了好几遍总是搞不清,即便是记公式也是收效不大.不妨按照书上总结一下. 绝对法: 无向图点度 ...
- Android学习——LinearLayout布局实现居中、左对齐、右对齐
android:orientation="vertical"表示该布局下的元素垂直排列: 在整体垂直排列的基础上想要实现内部水平排列,则在整体LinearLayout布局下再创建一 ...