一.题目描写叙述

二.解题技巧

这道题不存在复杂的分析过程和边界条件。假设单纯得考虑从小到大地将两个数组进行合并的话。每次在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的更多相关文章

  1. [LeetCode] 88. Merge Sorted Array 混合插入有序数组

    Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 as one sorted array. Note: T ...

  2. Leetcode#88. Merge Sorted Array(合并两个有序数组)

    题目描述 给定两个有序整数数组 nums1 和 nums2,将 nums2 合并到 nums1 中,使得 num1 成为一个有序数组. 说明: 初始化 nums1 和 nums2 的元素数量分别为 m ...

  3. LeetCode 88. Merge Sorted Array(合并有序数组)

    Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 as one sorted array. Note:Yo ...

  4. [LeetCode] 88. Merge Sorted Array 合并有序数组

    Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 as one sorted array. Note: T ...

  5. 【leetcode】Merge Sorted Array

    题目描述 Given two sorted integer arrays A and B, merge B into A as one sorted array. Note: You may assu ...

  6. LeetCode 88 Merge Sorted Array

    Problem: Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 as one sorted array ...

  7. 【题解】【数组】【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 ...

  8. 【leetcode】Merge Sorted Array(合并两个有序数组到其中一个数组中)

    题目: Given two sorted integer arrays A and B, merge B into A as one sorted array. Note: You may assum ...

  9. leetcode[89] Merge Sorted Array

    合并两个有序数组,放在A中,A中的空间足够. Given two sorted integer arrays A and B, merge B into A as one sorted array. ...

  10. Leetcode 88. Merge Sorted Array(easy)

    Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 as one sorted array. Note:Yo ...

随机推荐

  1. 决策树(Decision Trees)

    简介 决策树是一个预测模型,通过坐标数据进行多次分割,找出分界线,绘制决策树. 在机器学习中,决策树学习算法就是根据数据,使用计算机算法自动找出决策边界. 每一次分割代表一次决策,多次决策而形成决策树 ...

  2. pytorch 8 CNN 卷积神经网络

    # library # standard library import os # third-party library import torch import torch.nn as nn impo ...

  3. JS中的Set 与去重

    -----------------------Set 与去重----------------------- ES6 提供了新的数据结构 Set.它类似于数组,但是成员的值都是唯一的,没有重复的值.Se ...

  4. 【Codeforces Round #483 (Div. 2) C】Finite or not?

    [链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 有个性质. 如果p/q是分数的最简形式. 那么p/q能化成有限小数. 当且仅当q的质因数分解形式中只有质因子2和5 (且不能出现其他 ...

  5. maven 构建web项目

    Maven规定,必须创建以下几个Source Folder src/main/resources src/main/Java src/test/resources src/test/java 添加以上 ...

  6. ES设置查询的相似度算法

    similarity Elasticsearch allows you to configure a scoring algorithm or similarity per field. The si ...

  7. 那些年尝试过的效率工具之Total Commander

    昨天电脑文件很乱,想整理一下发现移动.复制文件要来回目录切换很麻烦,突然就又想起了用Total Commander——简称TC,很久之前尝试过但没坚持使用的工具. 借此机会总结一下自己对TC的认识,后 ...

  8. word2tex之类的问题

    首先就是这个word2tex一般是在word和tex文本互相转换的时候用的. 以前win7下用的chikrii忘了使用方法.. 之后再win10下用的excel2tex,但是转换时候总感觉不如word ...

  9. SpringCloud学习笔记(18)----Spring Cloud Netflix之服务网关Zuul原理

    1. Zuul的工作机制 Zuul提供了一个框架,可以对过滤器进行动态的加载,编译,运行.过滤器之间没有直接的相互通信,他们是通过一个RequestContext的静态类来进行数据传递的.Requet ...

  10. 优动漫PAINT-简单的树、叶教学

    如题,简单.好用:其实说的还是一个观察的事.看你是否足够细心,对于树叶的生长.枝桠和树干的关系是否了解咯. 对于这样的树枝丫和叶子完全可以使用优动漫PAINT完成,简单又快捷,软件下载:www.don ...