当前位置:网站首页>面试题 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;
}
边栏推荐
- CDGA|从事通信行业,那你应该考个数据管理证书
- Detailed explanation of JUnit unit test framework
- 记一次 .NET 差旅管理后台 CPU 爆高分析
- 有关 M91 快速霍尔测量仪的更多信息
- Intensive cultivation of channels for joint development Fuxin and Weishi Jiajie held a new product training conference
- 线程的并行、并发、生命周期
- [English grammar] Unit1 articles, nouns, pronouns and numerals
- Dom4J解析XML、Xpath检索XML
- uni-app微信小程序一键登录获取权限功能
- uni-app商品分类
猜你喜欢

Dom4j parsing XML, XPath retrieving XML

Lumiprobe free radical analysis h2dcfda instructions

Digital business cloud: from planning to implementation, how does Minmetals Group quickly build a new pattern of digital development?

Solution and summary of Nacos startup failure

研究了11种实时聊天软件,我发现都具备这些功能…

论文泛读【FiLM: Visual Reasoning with a General Conditioning Layer】

C-end dream is difficult to achieve. What does iFLYTEK rely on to support the goal of 1billion users?

How to solve the problem of splash screen when the main and sub code streams of easygbs are h.265?

Lake Shore—OptiMag 超导磁体系统 — OM 系列

奔赴山海之前,毕业季一定要做的那些事情
随机推荐
DTD建模
Go Language Advanced
Lake Shore—CRX-EM-HF 型低温探针台
ddr4测试-2
学习笔记【gumbel softmax】
Lake Shore低温恒温器的氦气传输线
Ubuntu14 install MySQL and configure root account local and remote access
Parallelism, concurrency and life cycle of threads
【org.slf4j.Logger中info()方法】
The use of subplot function in MATLAB
Introduction to relevant processes and functions of wechat official account development
MFC中如何重绘CListCtrl的表头
寶,運維100+服務器很頭疼怎麼辦?用行雲管家!
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
Task: denial of service DOS
Summary of SQL query de duplication statistics methods
EasyGBS主子码流都为H.265时,切换出现花屏如何解决?
sql查询去重统计的方法总结
XML syntax, constraints
More information about M91 fast hall measuring instrument