当前位置:网站首页>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;
}
边栏推荐
- 商业智能BI开发和报表开发有什么本质区别?
- The intelligent epidemic prevention system provides safety guarantee for the resumption of work and production at the construction site
- Transform + ASM data
- Introduction and installation of crunch, and making password dictionary with crunch
- IPv4地址、子网掩码、网关
- Les canaux de culture intensive s'efforcent de développer Fu Xin et Wei Shi jiajie pour organiser une conférence de formation sur les nouveaux produits
- 学习笔记-JDBC连接数据库操作的步骤
- Bao, que se passe - t - il si le serveur 100 + O & M a mal à la tête? Utilisez le majordome xingyun!
- 正则表达式=Regex=regular expression
- Extensive reading of the paper [film: visual reasoning with a general condition layer]
猜你喜欢

论文阅读【Discriminative Latent Semantic Graph for Video Captioning】

商业智能BI开发和报表开发有什么本质区别?

Bao, what if the O & M 100+ server is a headache? Use Xingyun housekeeper!

ECS summer money saving secret, this time @ old users come and take it away

数字化转型企业成功的关键,用数据创造价值

为什么一定要从DevOps走向BizDevOps?

GB28181之SIP协议

如何正确使用Vertx操作Redis(3.9.4带源码分析)

论文阅读【Learning to Discretely Compose Reasoning Module Networks for Video Captioning】
Use the uni app demo provided by Huanxin to quickly realize one-on-one chat
随机推荐
Bao, what if the O & M 100+ server is a headache? Use Xingyun housekeeper!
Ubuntu14 install MySQL and configure root account local and remote access
【org.slf4j.Logger中info()方法】
DTD建模
sql查询去重统计的方法总结
Detailed explanation of JUnit unit test framework
Learning records of building thingsboard, an Internet of things platform
研究了11种实时聊天软件,我发现都具备这些功能…
A brief understanding of white box encryption technology
ddr4测试-2
云服务器ECS夏日省钱秘籍,这次@老用户快来领走
直播HLS协议
[Mori city] random talk on GIS data (I)
MFC中如何重绘CListCtrl的表头
torch.nn.functional.interpolate函数
正则表达式=Regex=regular expression
Thesis reading [distinctive late semantic graph for video capturing]
物联网平台thingsboard搭建学习记录
Nat penetration of gb28181
DTD modeling