当前位置:网站首页>String replacement related topics (merging arrays)
String replacement related topics (merging arrays)
2022-04-22 08:36:00 【Sunspot ball】
1、 Title Description :
There are two sort arrays A1 and A2, In memory A1 There is enough free space at the end of the to accommodate A2. Please implement a function , hold A2 Insert all numbers in... Into A1 in , And all the numbers are sorted .
2、 The test case
3、 Solutions
Train of thought : Scan string from beginning to end , Replace every time you encounter a space character . But because it is 1 Replace the characters with 3 Characters , You have to move all the characters after the space back 2 byte .
Train of thought two : Merging two arrays ( Including strings ) when , If you assign each number from front to back ( Or character ), You need to move the number repeatedly ( Or character ) many times , So we can consider copying from the back to the front , This reduces the number of moves , To improve efficiency .
Complexity analysis : Time complexity O(N^2)
The code is as follows :
#include <stdio.h>
#include <string.h>
void Unite_Arry(int *arrA, int *arrB, int len1, int len2)
{
// Create a new array , Used to store the reorganized elements
int arrC[30] = {
0};
//arrC Length of array
int len3 = len1 + len2;
int len = len3;
// Enter judgment
if(NULL == arrA || NULL == arrB || (len1-1) < 0 || (len2-1) < 0)
{
printf("The input parameter error!\n");
return;
}
// Start array reorganization
while((len1-1) >= 0 && (len2-1) >= 0)
{
if(arrA[len1-1] >= arrB[len2-1])
{
arrC[lem3-1] = arrA[len1-1];
len1--;
len3--;
}
else
{
arrC[len3-1] = arrB[len2-1];
len2--;
len3--;
}
}
// When arrA Element ratio of array arrB When there are many elements in the array , Copy the extra elements directly to arrC Foremost
if(len1 > 0)
{
memcpy(arrC, arrA, len1*sizeof(int));
}
// When arrB Element ratio of array arrA When there are many elements in the array , Copy the extra elements directly to arrC Foremost
if(len2 > 0)
{
memcpy(arrC, arrB, len2*sizeof(int));
}
// Output the reorganized array
int i;
printf("Reorganized arrC is:");
for(i=0;i<len;i++)
{
printf("%d ", arrC[i]);
}
printf("\n");
}
int main()
{
int arrA[] = {
1,3,6,9,10};
int arrB[] = {
0,1,2,12};
int n,m;
printf("Original arrA is:");
for(n=0; n<sizeof(arrA)/sizeof(int); n++)
{
printf("%d ", arrA[n]);
}
printf("\n");
printf("Original arrB is:");
for(m=0; m<sizeof(arrB)/sizeof(int); m++)
{
printf("%d ", arrB[m]);
}
printf("\n");
Unite_Arry((int*)arrA, (int*)arrB, sizeof(arrA)/sizeof(int), sizeof(arrB)/sizeof(int));
return 0;
}
版权声明
本文为[Sunspot ball]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204220742498207.html
边栏推荐
猜你喜欢
随机推荐
Handler related source code analysis
【大话云原生】微服务篇-五星级酒店的服务方式
CentOS 安裝 MySQL
SQL数据库选择题(2)
【论文阅读】【3d目标检测】Voxel Set Transformer: A Set-to-Set Approach to 3D Object Detection from Point Clouds
指针和字符串
JMU enumeration weekday
Probability theory note 6.3 sampling distribution
shell监控IBM MQ队列深度,10s扫描三次,有两次以上深度值超过5时,则输出队列名称和深度值。
[跟着官方文档学JUnit5][二][WritingTests][学习笔记]
235. 二叉搜索树的最近公共祖先(Easy)
liunx基础—zabbix5.0监控系统安装部署
Teach you how to realize the pull-down refresh and pull-up load of recyclerview
WeChat official account - webpage authorization
地图裁剪器,可以将图片裁剪成瓦片数据,主要用途是将高清卫星图像裁剪成瓦片图,可以做离线地图的开发,基于墨卡托坐标
Viewpager comprehensive summary
第2关:子节点创建、列出、删除
Handler相关源码分析
JSON数据文本
算法--删除链表的倒数第 N 个结点(Kotlin)




![[paper reading] [3D target detection] pvgnet](/img/fc/7fd22bbc1aaf4bc5ae38543cd03ae3.png)




