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. bzoj1004 [HNOI2008]Cards Burnside定理+背包

    题目传送门 思路:首先是Burnside引理,要先学会这个博客. Burnside引理我们总结一下,就是 每种置换下不动点的数量之和除以置换的总数,得到染色方案的数量.        这道题,显然每种 ...

  2. OPENROWSET read excel

    由于64位系统已经不支持 oledb 4.0访问 xls 1. 未在本地计算机上注册“microsoft.ACE.oledb.12.0”提供程序 去http://download.microsoft. ...

  3. web服务器架构演化及所其技术知识体系(分布式的由来)

    文章标题是我自己取的,内容来着百度百科k5665219的一篇回答,觉得讲的很不错就转载过来了. 最开始,由于某些想法,于是在互联网上搭建了一个网站,这个时候甚至有可能主机都是租借的,但由于这篇文章我们 ...

  4. PHP冒泡排序的实现方法

    <?php function BubbleSort($arr){ $count = count($arr); if($count<=1){ return $arr; } for($i=0; ...

  5. Tor网络介绍

    Tor网络介绍 1.Tor的全称是“The Onion Router”,“An anonymous Internet communicaton system:通过Tor访问一个地址时,所经过的节点在T ...

  6. 1.Vue.js的常用指令

      Vue.js介绍 Vue.js是当下很火的一个JavaScript MVVM库,它是以数据驱动和组件化的思想构建的.相比于Angular.js,Vue.js提供了更加简洁.更易于理解的API,使得 ...

  7. DIV滚动样式

    .divScroll{     OVERFLOW:auto;        scrollbar-face-color: #FFFFFF;        scrollbar-shadow-color: ...

  8. asp.net的几种页面传值方式

    1."~/x/xx.aspx?id=" + id string id=Request.Params["id"].ToString(); 2.Response.R ...

  9. 2017年9月22日 关于JS数组

    JS数组 JS数组的定义方法 var arr = []; var arr = new Array() JS数组属性 长度 arr.length 遍历数组 索引值:从0开始数 第一种方法 for(var ...

  10. Scala 知识点掌握1

    Scala知识点巩固 1.Scala简介 Scala是一门面向对象和面向函数的编程语言,是一门静态编程语言,如 Java Scala(变量类型在编译阶段确定):源码文件需要基于 JVM 运行的. 动态 ...