当前位置:网站首页>Niuke: merging two ordered arrays
Niuke: merging two ordered arrays
2022-06-21 18:57:00 【lsgoose】


The idea of merging here is very simple , Is to compare the size of the elements .
But there are a few details to note , We put it from the back to the front , Because if from now on, if B The elements of are occupying A Front position , that A The element of is thrown away .
secondly , Be aware that when you finally deal with unsorted elements , Only B It needs to be handled before it is finished , because A If you haven't finished , and B It's over ,A In fact, the previous elements can be kept in place .
The code is as follows :
class Solution {
public:
void merge(int A[], int m, int B[], int n) {
int i=m-1;
int j=n-1;
int k=m+n-1;
while(i>=0 && j>=0){
if(A[i]>B[j]){
A[k--]=A[i--];
}else{
A[k--]=B[j--];
}
}
if(i<0){
while(j>=0){
A[k--]=B[j--];
}
}
}
};边栏推荐
- Listener and filter (monitor and interceptor) in Servlet
- Canvas dynamic mesh background JS effect
- How to apply for SSL certificate using IP
- C language__ attribute__ (packed) attribute (learn)
- STATA 速查备份(个人笔记)
- Three. JS 3D particle animation JS special effect code
- Character processing of node
- SVG左上角全屏菜单动画效果展开
- C1—Qt实现简易计算器的思路2021.10.15
- EL表达式
猜你喜欢

Threejs aircraft Earth 3D scene animation

Cookies and sessions

ACL 2022 | 基于自监督图对齐的多语言知识图谱推理

【Go语言】Go语言我们应该这样学~全网较全的学习教程

Must the database primary key be self incremented? What scenarios do not suggest self augmentation?

RK3566调试GC2053

网络爬虫开发工具:Screaming Frog SEO Spider

新赛季的中超和国安,荆棘中前行

文件上传漏洞靶场分析 UPLOAD_LABS

Servlet中Listener与Filter (监视器与拦截器)
随机推荐
Canvas球体粒子变幻颜色js特效
让接口自动化测试灵动起来
力扣142. 环形链表 II
挖财学堂属于证券公司吗?开户安全吗?
Must the database primary key be self incremented? What scenarios do not suggest self augmentation?
Day19QPushButton的使用2021-10-30
一篇文章彻底学会画数据流图
AWS device shadow usage
AWS Device Shadow使用
Day15Qt中字符串的常用操作2021-10-20
剑指Offer 56. 删除链表的重复节点
Canvas sphere particle changing color JS special effect
8. get directory function / get file function -dir / -notdir
Canvas dynamic mesh background JS effect
外资上演“胜利大逃亡”、内资接盘,新东方在线“方”了?
Module import method of node
C language__ attribute__ (packed) attribute (learn)
左右两侧垂直带序号的时间轴
Make interface automation test flexible
How to use DBA_ hist_ active_ sess_ History analysis database history performance problems