【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 ...
随机推荐
- Dear friends:
To realize the value of ONE YEAR想知道一整年的价值ask the student who has failed a class就去问被当过的学生To realize ...
- 【网络】访问控制列表(ACL)
目的:172.16.12.1可以telnet到172.16.12.254,但是无法ping通172.16.12.254 Router0配置: 配置enable密码(必须,否则无法进入特权模式): r1 ...
- Intellij IDEA 添加项目依赖
https://blog.csdn.net/TaooLee/article/details/51305443 idea导入一个maven项目 https://blog.csdn.net/qq_3837 ...
- vue2.0 在main.js引入scss文件报错
在vue2.0的main.js中引入scss文件报错 原因是在 在build文件夹下的webpack.base.conf.js的rules里面添加配置 { test: /\.scss$/, loade ...
- springboot+Zookeeper+Dubbo入门
最近想学习dubbo,就利用周末写了一个特别简单的demo,不知道有没有用,先记录一下. 1.安装zookeeper并启动(安装看我上一篇博客https://www.cnblogs.com/huang ...
- Python获取当前时间及格式化
1.导入time模块 # 导入time模块 import time 2.打印时间戳-time.time() # 导入time模块 import time # 打印时间戳 print(time.time ...
- Java中forEach, 用来遍历数组
这里的for是Java中forEach, 用来遍历数组的.for(int i : d) 就是遍历int型数组d的 每一次访问数组d的时候读取的数据放入int型的i中.和for(int i=0;i< ...
- Fragment、Activity比较——Android碎片介绍
Fragment是Android honeycomb 3.0新增的概念,Fragment名为碎片不过却和Activity十分相似,下面介绍下Android Fragment的作用和用法.Fragmen ...
- 【转】使用Python的Requests库进行web接口测试
原文地址:使用Python的Requests库进行web接口测试 1.Requests简介 Requests 是使用 Apache2 Licensed 许可证的 HTTP 库.用 Python 编写, ...
- matlab矩阵中如何去掉重复的行;如何找到相同的行,并找到其位置
找到了2个函数:unique和ismember 1. 去掉其中的重复行:unique 例子: IDX = [,,; ,,; ,,; ,,; ,,; ,,]; classNo = unique(IDX, ...