【Leetcode】【Easy】Merge Sorted Array
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的更多相关文章
- LeetCode Array Easy 88. Merge Sorted Array
Description Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 as one sorted ar ...
- 88. Merge Sorted Array【easy】
88. Merge Sorted Array[easy] Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 ...
- 【LeetCode练习题】Merge Sorted Array
Merge Sorted Array Given two sorted integer arrays A and B, merge B into A as one sorted array. Note ...
- 【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 ...
- 【一天一道LeetCode】#81. Search in Rotated Sorted Array II
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Follow ...
- 【一天一道LeetCode】#80. Remove Duplicates from Sorted Array II
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Follow ...
- 【LeetCode题意分析&解答】40. Combination Sum II
Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in ...
- 【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 ...
- 【LeetCode题意分析&解答】37. Sudoku Solver
Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells are indicated by th ...
- 【刷题】Search in a Big Sorted Array
原题戳我. 题目 Description Given a big sorted array with positive integers sorted by ascending order. The ...
随机推荐
- 单个html使用axios调用接口传参
单个html页面使用axios调用接口传参(没有使用v-cli搭建框架,也没有使用qs等等) 1.使用 URLSearchParams的方法 var params = new URLSearchPar ...
- [转] CSS 选择器参考手册
[From] http://www.w3school.com.cn/cssref/css_selectors.asp CSS3 选择器 在 CSS 中,选择器是一种模式,用于选择需要添加样式的元素. ...
- PIE SDK矢量数据的读取
1.功能简介 GIS将地理空间数据表示为矢量数据和栅格数据.矢量数据模型使用点.线和多边形来表示具有清晰空间位置和边界的空间要素,如控制点.河流和宗地等,每个要素被赋予一个ID,以便与其属性相关联.栅 ...
- oracle12C--DG FAR SYNC 部署(前提为搭建好12C的DG)
<<往期12CDG搭建>> 一,理解同步异步模式 01, 使用LGWR 进程的SYNC 方式 1)Primary Database 产生的Redo 日志要同时写到日志文件和网络 ...
- oracle 基础知识(一)
Oracle 用户.权限.角色管理 01.概念 用户:对数据库的访问需要以适当的身份通过验证,这就是用户的作用:每个Oracle用户都有自己的用户名和密码,并且拥有他们所创建的任意表.视图和其他资源, ...
- shell 脚本学习之内部变量
一,$BASH Bash的二进制程序文件的路径 二,$BASH_ENV 这个环境变量会指向一个Bash的启动文件, 当一个脚本被调用的时候, 这个启动文件将会被读取. 三,$BASH_SUBSHELL ...
- 【3dsMax安装失败,如何卸载、安装3dMax 2015?】
AUTODESK系列软件着实令人头疼,安装失败之后不能完全卸载!!!(比如maya,cad,3dsmax等).有时手动删除注册表重装之后还是会出现各种问题,每个版本的C++Runtime和.NET f ...
- 问题1-xshell远程连接不上linux主机
在其他主机上搭建linux系统,我们一般通过xshell工具去远程访问该主机,这样不仅可以方便我们的对系统或者集群进行管理也方便了我们的操作,但是在搭建好环境的时候遇到如下问题: 解决方案:1.关闭目 ...
- OJ (Online Judge)使用
这是一种方式,我们还可使用另外一种方式: process.stdin.resume(); process.stdin.setEncoding('ascii'); var input = "& ...
- 安装Drupal
我在虚拟机里面安装了Ubuntu Server 14.参考https://www.digitalocean.com/community/tutorials/how-to-install-drupal- ...