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. POJ_1019 Number Sequence 【递推】

    题目: A single positive integer i is given. Write a program to find the digit located in the position ...

  2. 基于 bootstrap html 响应式 布局

    <!DOCTYPE html> <html lang="zh-CN"> <head> <meta charset="UTF-8& ...

  3. bzoj3196 二逼平衡树 树状数组套线段树

    题目传送门 思路:树状数组套线段树模板题. 什么是树状数组套线段树,普通的树状数组每个点都是一个权值,而这里的树状数组每个点都是一颗权值线段树,我们用前缀差分的方法求得每个区间的各种信息, 其实关键就 ...

  4. 解决页面使用overflow: scroll,overflow-y:hidden在iOS上滑动卡顿的问题

    解决页面使用overflow: scroll,overflow-y:hidden在iOS上滑动卡顿的问题 div{ width: 100%; overflow-y: hidden; -webkit-o ...

  5. python开头——文件声明 详解

    一.解释器声明 1.声明方式 linux #!/usr/bin/python windowns #!c:/python27/python.exe 放在首行 2.作用 告诉电脑,要用/usr/bin下面 ...

  6. 获取window.location.href路径参数

    GetQueryString(param) { //param为要获取的参数名 注:获取不到是为null var currentUrl = window.location.href; //获取当前链接 ...

  7. Linux-Xshell5

    Linux-Xshell5 1.下载 2.安装 3.新建会话(连接) 点击新建 需要知道要连接的 IP, 查看命令   ifconfig 配置 名称可以自己命名,主机写要连接的 IP,其他的不能改 输 ...

  8. 2019.03.26 读书笔记 关于event

    event 主要是给委托加了一层保护,不能任意的 class.delegate=null,class.delegate=fun1,不能由调用者去任意支配,而是由class自己去增加或减少,用+=.-= ...

  9. python3初探

      官方网站:https://www.python.org/ 类库大全:https://pypi.python.org/pypi 基础类库:https://docs.python.org/3/libr ...

  10. 网页引用Font Awesome图标

    问题:最近在IIS上部署web项目的时候,发现浏览器总是报找不到woff.woff2字体的错误.导致浏览器加载字体报404错误,白白消耗了100-200毫秒的加载时间. 原因:因为服务器IIS不认SV ...