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. HDU 3783

    读入一个字符串,字符串中包含ZOJ三个字符,个数不一定相等,按ZOJ的顺序输出,当某个字符用完时,剩下的仍然按照ZOJ的顺序输出. MY:(OUTPUT LIMIT EXCEED) #include& ...

  2. AtCoder Beginner Contest 113 C

    C - ID Time limit : 2sec / Memory limit : 1024MB Score: 300 points Problem Statement In Republic of ...

  3. UVA - 10298 后缀数组(仅观赏)

    题意:求最小循环节 \(KMP\)可以20ms通过,而\(da\)实现的后缀数组并无法在3000ms内通过 听说要用\(dc3\)才勉强卡过,这里仅列出\(da\)实现 #include<ios ...

  4. PHP中Session ID的实现原理

    前序: 首先要明白PHPSESSID看似多次刷新都不会改变其实是没有删除本地相关联的cookie,删除的方法 session_destroy();//删除服务器端的session文件 setcooki ...

  5. Selenium => Debugging “Element is not clickable at point” error

    [From] http://stackoverflow.com/questions/11908249/debugging-element-is-not-clickable-at-point-error ...

  6. maven项目在eclipse中debug时看不到源码?

    就像图中一样,看不到源码,但是能step over,也可查看变量值,点击edit source lookup path,选定项目的一瞬间源码会出来,但马上又变回原样了,求大神指教~ 我也遇到这个问题了 ...

  7. centos 7更新yum源与更新系统

    1.备份/etc/yum.repos.d/下的原有文件(假如你想保留原系统的yum源的话),举例 cp /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos ...

  8. poj3262

    一.题意:有n头牛,每头牛每分钟会吃D个菜,把这头牛赶回去需要时间T(人再返回又需要T),一次只能赶回去一头牛,也就是说剩下的牛会继续吃菜.求牛最少吃多少菜 二.思路:贪心.按D/T将牛进行排序,然后 ...

  9. jenkins集成python脚本

    Jenkins配置获取源码并构建 1. Jenkins系统设置--管理插件,搜索安装“GitLab”和“Git” 2. 新建任务,选择自由风格项目 3. 源码管理 git (1)Repository ...

  10. APP在实际开发中应注意的关键点

    在APP开发过程中,开发者比较注重的是功能模块的实现,从而忽略了APP的设计问题,特别是企业开发APP,但是,APP设计是APP开发中非常重要的一个环节,APP界面设计直接影响到APP用户的感官,因此 ...