当前位置:网站首页>面试题 16.16. 部分排序-双指针法
面试题 16.16. 部分排序-双指针法
2022-07-01 18:45:00 【Mr Gao】
面试题 16.16. 部分排序
给定一个整数数组,编写一个函数,找出索引m和n,只要将索引区间[m,n]的元素排好序,整个数组就是有序的。注意:n-m尽量最小,也就是说,找出符合条件的最短序列。函数返回值为[m,n],若不存在这样的m和n(例如整个数组是有序的),请返回[-1,-1]。
示例:
输入: [1,2,4,7,10,11,7,12,6,7,16,18,19]
输出: [3,9]
解题代码如下:
/** * 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;
}
边栏推荐
- MySQL常用图形管理工具 | 黑马程序员
- More information about M91 fast hall measuring instrument
- [go ~ 0 to 1] day 4 June 30 defer, structure, method
- 研究了11种实时聊天软件,我发现都具备这些功能…
- Lake Shore—OptiMag 超导磁体系统 — OM 系列
- Lake Shore 连续流动低温恒温器传输线
- Download (export) PDF template file (such as approval form), and report error: invalid nested tag * * * found, expected closing tag***
- Lumiprobe phosphide hexaethylene phosphide specification
- [info() method in org.slf4j.logger]
- Solidity - truncated and checked modes of arithmetic operations - new features of 0.8.0
猜你喜欢
sql查询去重统计的方法总结
Parallelism, concurrency and life cycle of threads
原生js打造日程表-支持鼠标滚轮滚动选择月份-可以移植到任何框架中
从零开始学 MySQL —数据库和数据表操作
ECS summer money saving secret, this time @ old users come and take it away
有关 M91 快速霍尔测量仪的更多信息
【森城市】GIS数据漫谈(一)
The former 4A executives engaged in agent operation and won an IPO
求各种极限的方法
Solidity - 算术运算的截断模式(unchecked)与检查模式(checked)- 0.8.0新特性
随机推荐
Team up to learn! 14 days of Hongmeng equipment development "learning, practicing and testing" practical camp, free of charge!
The market value evaporated by 74billion yuan, and the big man turned and entered the prefabricated vegetables
原生js打造日程表-支持鼠标滚轮滚动选择月份-可以移植到任何框架中
赋能「新型中国企业」,SAP Process Automation 落地中国
【英语语法】Unit1 冠词、名词、代词和数词
Go Language Advanced
Lake shore M91 fast hall measuring instrument
[pytorch record] distributed training dataparallel and distributeddataparallel of the model
Specification of lumiprobe reactive dye indocyanine green
CDGA|从事通信行业,那你应该考个数据管理证书
M91 fast hall measuring instrument - better measurement in a shorter time
PMP是被取消了吗??
使用环信提供的uni-app Demo,快速实现一对一单聊
混沌工程平台 ChaosBlade-Box 新版重磅发布
论文阅读【Learning to Discretely Compose Reasoning Module Networks for Video Captioning】
axure不显示元件库
【6.24-7.1】写作社区精彩技术博文回顾
Lake Shore—OptiMag 超导磁体系统 — OM 系列
Solidity - 算术运算的截断模式(unchecked)与检查模式(checked)- 0.8.0新特性
EasyGBS网络不稳定情况下重复请求视频拉流问题的优化