当前位置:网站首页>Interview question 16.16 Partial sorting - Double finger needling
Interview question 16.16 Partial sorting - Double finger needling
2022-07-01 19:35:00 【Mr Gao】
Interview questions 16.16. Partial sorting
Given an array of integers , Write a function , Find the index m and n, Just index the range [m,n] The elements of are in order , The whole array is ordered . Be careful :n-m As little as possible , in other words , Find the shortest sequence that meets the conditions . The return value of the function is [m,n], If there is no such m and n( For example, the whole array is ordered ), Please return [-1,-1].
Example :
Input : [1,2,4,7,10,11,7,12,6,7,16,18,19]
Output : [3,9]
The solution code is as follows :
/** * Note: The returned array must be malloced, assume caller calls free(). */
int* subSort(int* array, int arraySize, int* returnSize){
int low=0,high=arraySize-1;
int *re=(int *)malloc(sizeof(int)*2);
re[0]=-1;
re[1]=-1;
*returnSize=2;
if(arraySize<=1){
return re;
}
while(array[low]<=array[low+1]){
low++;
// printf("%d ",low);
if(low==arraySize-1){
return re;
}
}
while(array[high-1]<=array[high]){
high--;
if(high==0){
return re;
}
}
// printf("low high %d %d",low,high);
int i;
int min=999,max=-1000;
for(i=low;i<=high;i++){
if(array[i]<min){
min=array[i];
}
if(array[i]>max){
max=array[i];
}
}
// printf("min max %d %d",min,max);
while(array[low]>min){
if(low==0){
break;
}
low--;
if(low==0){
break;
}
}
while(array[high]<max){
if(high==arraySize){
break;
}
high++;
if(high==arraySize){
break;
}
}
if(low==0){
low--;
}
re[0]=low+1;
re[1]=high-1;
*returnSize=2;
return re;
}
边栏推荐
- Ffmpeg avframe to cv:: mat
- Go语言高级
- 使用环信提供的uni-app Demo,快速实现一对一单聊
- Write it down once Net travel management background CPU Explosion Analysis
- ffmpeg 音频相关命令
- 【英语语法】Unit1 冠词、名词、代词和数词
- wireshark报文分析tcp,ftp
- 研究了11种实时聊天软件,我发现都具备这些功能…
- B2B e-commerce platform solution for fresh food industry to improve the standardization and transparency of enterprise transaction process
- 音视频、编解码相关电子书、小工具,打包奉送!
猜你喜欢
EasyGBS网络不稳定情况下重复请求视频拉流问题的优化
Lumiprobe phosphide hexaethylene phosphide specification
赋能「新型中国企业」,SAP Process Automation 落地中国
测试自学人必看:软件测试如何找测试项目?
Intensive cultivation of channels for joint development Fuxin and Weishi Jiajie held a new product training conference
求各种极限的方法
Task: denial of service DOS
Specification of lumiprobe reactive dye indocyanine green
uni-app商品分类
如何正确使用Vertx操作Redis(3.9.4带源码分析)
随机推荐
OpenCV视频质量检测--清晰度检测
Crunch简介、安装,使用Crunch制作密码字典
Go Language Advanced
Implement a Prometheus exporter
Instagram 为何从内容共享平台变成营销工具?独立站卖家如何利用该工具?
Task: denial of service DOS
音视频、编解码相关电子书、小工具,打包奉送!
Solution and summary of Nacos startup failure
【pytorch记录】自动混合精度训练 torch.cuda.amp
Summary of SQL query de duplication statistics methods
论文阅读【Learning to Discretely Compose Reasoning Module Networks for Video Captioning】
Introduction and installation of crunch, and making password dictionary with crunch
H264编码profile & level控制
XML语法、约束
What must be done in graduation season before going to Shanhai
Ubuntu14 install MySQL and configure root account local and remote access
pickle.load报错【AttributeError: Can‘t get attribute ‘Vocabulary‘ on <module ‘__main__‘】
IPv4地址、子网掩码、网关
混沌工程平台 ChaosBlade-Box 新版重磅发布
从零开始学 MySQL —数据库和数据表操作