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.

解题思路:

1、合并两个有序数列,并且数列A已经具有充足的空间以供合并。

2、采取从后向前填入的方式,避免了空间冲突,o(n+m)

解题步骤:

1、新建三个变量,合并后数列的index,用于遍历a、b的aIndex、bIndex

2、循环开始,循环条件是aIndex和bIndex都不为0,因为需要比较后再插入:

  (1)将较大的一方插入后端,同时递减;

3、因为是在A上操作,当循环结束,而A还有剩余,就刚好放在原位置不变;如果B还有剩余,那么将B值复制到A中;

 class Solution {
public:
void merge(int A[], int m, int B[], int n) {
int index = m + n - ;
int aIndex = m - ;
int bIndex = n - ; while(aIndex >= && bIndex >= ) {
A[index--] = B[bIndex] > A[aIndex] ? B[bIndex--] : A[aIndex--];
} while(bIndex >= ) {
A[index--] = B[bIndex--];
}
}
};

另,

我第一次做的时候,写了很多代码来判断A和B的排序方式。根据不同的排序方式,比如同为由大到小或同为由小到大,或者相互不一样,对应的写不同的合并代码。

看了别人的解答后,才发现已经默认A和B都是由小到大排列的...不知道是如何从题目得知的...

【Leetcode】【Easy】Merge Sorted Array的更多相关文章

  1. LeetCode Array Easy 88. Merge Sorted Array

    Description Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 as one sorted ar ...

  2. 88. Merge Sorted Array【easy】

    88. Merge Sorted Array[easy] Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 ...

  3. 【LeetCode练习题】Merge Sorted Array

    Merge Sorted Array Given two sorted integer arrays A and B, merge B into A as one sorted array. Note ...

  4. 【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 ...

  5. 【一天一道LeetCode】#81. Search in Rotated Sorted Array II

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Follow ...

  6. 【一天一道LeetCode】#80. Remove Duplicates from Sorted Array II

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Follow ...

  7. 【LeetCode题意分析&解答】40. Combination Sum II

    Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in ...

  8. 【LeetCode题意分析&解答】35. Search Insert Position

    Given a sorted array and a target value, return the index if the target is found. If not, return the ...

  9. 【LeetCode题意分析&解答】37. Sudoku Solver

    Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells are indicated by th ...

  10. 【刷题】Search in a Big Sorted Array

    原题戳我. 题目 Description Given a big sorted array with positive integers sorted by ascending order. The ...

随机推荐

  1. CF248E Piglet's Birthday(概率dp)

    题面 luogu CodeForces 题解 \(orz\) yyb 转移蜜罐其实是吓唬人的...... 转移的蜜罐都是尝试过的,所有只关心当前架子上的蜜罐数 设\(f[i][j]\)表示第i个货架有 ...

  2. Laravel项目部署到Nginx服务器除了/目录,全飘404

    https://blog.csdn.net/yoywow/article/details/52153610 不管是Nginx还是Apache,如果不配置,都会出现404,不能路由. 我云服务器安装的是 ...

  3. SpringBoot 精简笔记

    0. Fundamental a. @SpringBootApplication //启动类 b. pom.xml //Maven基本依赖 <parent> <groupId> ...

  4. ecmall模板编辑中的标题如何自定义读取

    碰见了一个问题,刚上线的ecmall项目.客户说标题不要商城首页这四个字. 我去源码里找,找了半天才找到. 问题描述如下: 找到title的最原始模板themes\mall\tmall\top.htm ...

  5. Farey Sequence(欧拉函数板子题)

    题目链接:http://poj.org/problem?id=2478 Farey Sequence Time Limit: 1000MS   Memory Limit: 65536K Total S ...

  6. Python Fileinput 模块

    作者博文地址:http://www.cnblogs.com/liu-shuai/ fileinput模块提供处理一个或多个文本文件的功能,可以通过使用for循环来读取一个或多个文本文件的所有行. [默 ...

  7. Unity3D游戏轻量级xlua热修复框架

    Unity3D游戏轻量级xlua热修复框架   一 这是什么东西 前阵子刚刚集成xlua到项目,目的只有一个:对线上游戏C#逻辑有Bug的地方执行修复,通过考察xlua和tolua,最终选择了xlua ...

  8. JSON转C#实体类

    https://www.bejson.com/convert/json2csharp/

  9. Linux利器strace

    strace常用来跟踪进程执行时的系统调用和所接收的信号. 在Linux世界,进程不能直接访问硬件设备,当进程需要访问硬件设备(比如读取磁盘文件,接收网络数据等等)时,必须由用户态模式切换至内核态模式 ...

  10. java线程中的 sleep() wait() yield()

    sleep()方法是让线程休眠  可以指定时间  其实就是让线程进入阻塞状态  指定的时间过后 进入就绪状态  不释锁 相当于抱着锁睡觉 wait()  让线程进入等待状态  被唤醒后才会继续执行   ...