【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 ...
随机推荐
- POJ3322 Bloxorz I 无脑广搜(我死了。。。)
多测不清空,爆零两行泪....我死了QWQ 每个节点3个状态:横坐标,纵坐标,和方向 说一下方向:0:立着,1:竖着躺着,上半部分在(x,y),2:横着躺着,左半部分在(x,y) 然后就有了常量数组: ...
- 下载 生成 requirements
生成你项目的所有 组件,模块 pip3 freeze > requirements.txt 下载requirements.txt 里的所有 模块 pip3 install -r requirem ...
- Putty使用帐号和密码的自动登录
Putty使用ssh key做验证登陆是最方便的,不用密码.如果不想做key exchange,只是单纯想保存帐号密码做自动登陆,可以借助bat文件的方式如下,其中MyServer是已经保存了的ses ...
- android主线程ActivityThread
ActivityThread在Android中它就代表了Android的主线程,但是并不是一个Thread类. 源码如下: http://androidxref.com/6.0.0_r1/xref/f ...
- Docker原理(开发技术分享转发)
Docker原理Docker是啥Docker是一个程序运行.测试.交付的开放平台,Docker被设计为能够使你快速地交付应用.在Docker中,你可以将你的程序分为不同的 基础部分,对于每一个基础部分 ...
- Zoj 3870——Team Formation——————【技巧,规律】
Team Formation Time Limit: 3 Seconds Memory Limit: 131072 KB For an upcoming programming contes ...
- C# 深入理解String
关于C#中的类型 在C#中类型分为值类型和引用类型,引用类型和值类型都继承自System.Object类,几乎所有的引用类型都直接从System.Object继承,而值类型具体一点则继承System. ...
- lua 遍历table
lua中有四种主要的遍历一个table的方法. 第一种方法: for key, value in pairs(testtb) do xxxx end 这种方法是按照key哈希后的顺序遍历的.比如下面代 ...
- java中json解析,xml解析
抓取网页内容,会返回json或者xml(html)格式的数据. 为了方便的对上述两种格式的数据进行解析,可采用解析工具. JsonPath https://github.com/jayway/Json ...
- vs文件属性(生成操作)各选项功能
右击项目里的文件,选择属性(F4)会有[生成操作]的选项. 它提供了14项选择,如图: 在这说一下常用的选项: 1.编译 编译用于c#代码类的操作,编译之后输出在该程序集的bin目录下.换句话说,代码 ...