当前位置:网站首页>合并两个有序的数组
合并两个有序的数组
2022-04-22 23:43:00 【月半的人】
链接:合并两个有序的数组__牛客网
来源:牛客网
给出一个有序的整数数组 A 和有序的整数数组 B ,请将数组 B 合并到数组 A 中,变成一个有序的升序数组
数据范围: 0≤n,m≤1000 \le n,m \le 1000≤n,m≤100,∣Ai∣<=100|A_i| <=100∣Ai∣<=100, ∣Bi∣<=100|B_i| <= 100∣Bi∣<=100
注意:
1.保证 A 数组有足够的空间存放 B 数组的元素, A 和 B 中初始的元素数目分别为 m 和 n,A的数组空间大小为 m+n
2.不要返回合并的数组,将数组 B 的数据合并到 A 里面就好了,且后台会自动将合并后的数组 A 的内容打印出来,所以也不需要自己打印
3. A 数组在[0,m-1]的范围也是有序的
import java.util.*;
public class Solution {
public void merge(int A[], int m, int B[], int n) {
}
}
首先我们要知道,数组A已经被扩容了,同时我们这个题有两个思路,:
第一,合并+排序
这个方法就是需要将B数组都转移至数组A,然后我们在进行排序
这个方法虽然简单,但是效率不是很高。所以我们就得要换一种方法。
第二,双指针思想(合并+排序同时进行)
首先我们要设置三个指针,一个指针指向两个原数组,另外一个指针指向新的数组,这样我们就可以让指向的元素进行比较,较小或者较大的元素先进行移值(前提是这两个数组是有序的)。
但是有个比较的重要的是,我们的新的数组是从头开始进行移值还是,从尾开始?
其实我们这道题已经设置好了,只能从后往前,原因是我们的新的数组是从原来的旧的数组上扩容得到的,从前往后就会将原数组的前面的元素进行覆盖,导致数据丢失。
int i =m-1;
int k = n-1;
int t = m+n-1;
while(i>=0&&k>=0){
if(A[i]>B[k]){
A[t] = A[i];
i--;
t--;
}else{
A[t] = B[k];
k--;
t--;
}
}
所以我们就这样进行设计。
但是我们还有一种情况需要考虑,就是当我们其中一组数据结束后,另外一组数据并不知道是否也全部移值好,所以,这时我们就需要在进行判断,如果没有拍好,那就将剩下的全部按顺序拍进去(原本就是有序数组)。
if(i>=0){
while(i>=0){
A[t] = A[i];
i--;
t--;
}
}
if(k>=0){
while(k>=0){
A[t] = B[k];
t--;
k--;
}
}
所以总的代码段就是:
import java.util.*;
public class Solution {
public void merge(int A[], int m, int B[], int n) {
int i =m-1;
int k = n-1;
int t = m+n-1;
while(i>=0&&k>=0){
if(A[i]>B[k]){
A[t] = A[i];
i--;
t--;
}else{
A[t] = B[k];
k--;
t--;
}
}
if(i>=0){
while(i>=0){
A[t] = A[i];
i--;
t--;
}
}
if(k>=0){
while(k>=0){
A[t] = B[k];
t--;
k--;
}
}
}
版权声明
本文为[月半的人]所创,转载请带上原文链接,感谢
https://blog.csdn.net/weixin_61652218/article/details/124327181
边栏推荐
- VI / VIM editor basic operation
- 【超能力】我想暂停时空
- Pytorch convolution kernel filling and stride, multiple input and multiple output channels, pool layer
- LabVIEW控制电脑关机、休眠、注销和重启
- On Spartacus product details page, use outlet to display user-defined data
- websoket封装版 参数配置化 开箱即用
- [*CTF2022]web题目复现及wp
- 浅谈LD_PRELOAD劫持
- 【leetcode】二叉树,654最大二叉树,105根据前序与中序遍历构造二叉树,106根据中序与后序遍列构造二叉树,889根据前序和后序遍历构造二叉树
- Introduction to opencv (II) -- affine transformation and perspective transformation
猜你喜欢

grid_map(6):grid-mapping-in-ROS编译运行
![[turtle confession collection]](/img/97/6f1008386931bcb93655d94121e2c4.png)
[turtle confession collection] "the moon at the bottom of the sea is the moon in the sky, and the person in front of us is the sweetheart." More joy and peace for the rest of your life ~ (with 3 sourc

JS calculate the circumference and area of the circle

程序设计语言基础(1)
![[leetcode refers to the path with a certain value in offer 34. Binary tree (medium)]](/img/3d/efb4114ba00a72ef69089445b15e7f.png)
[leetcode refers to the path with a certain value in offer 34. Binary tree (medium)]

Excel VBA multi condition screening and summary statistics
![[perseverance challenge] PCIe asks and answers every day (2022.04 in progress)](/img/15/a63bfcc74ee4c6f4154bc832472afb.png)
[perseverance challenge] PCIe asks and answers every day (2022.04 in progress)

分享两道最近做的比较经典的OJ题(排列子序列+字符串中找出连续最长的数字串)

【DVCon2020】软件兄弟呐喊:硬件兄弟,请你做个人

Conjugate gradients (4)
随机推荐
How to quickly reprint blogs in CSDN
Complete set of JUC (1)
The parameter configuration of websoket package is out of the box
[experience sharing] share mangopapa's paper learning experience
Mung bean sprouts on the 22nd day of home
MPP架构概念
人们对于产业互联网的认识越来越清晰,越来越接近产业互联网
一个关于混淆的 Native 崩溃分析
程序设计语言基础(1)
【经验分享】分享 MangoPapa 的论文学习经验
FileNotFoundError: [Errno 2] No such file or directory: 'image/1. Jpg 'problem solving
[PCIe actual combat] SNPs PCIe enables SRIS mode
Esp32 (GPIO) - GPIO learning (5)
[leetcode sword finger offer 36. Binary search tree and two-way linked list (medium)]
51 单片机学习_4-2 数码管动态显示
[leetcode] binary tree, in-depth understanding of the first, middle and last order
2 technical HR offers, series
PCIe 参考时钟架构 (Refclk Architecture)
【newcoder】20220422周赛
(JS) use the properties and methods of the string object to realize the registration and login functions. The length of the user name is required to be in the range of 3-10 and the password is 6-20 ch