当前位置:网站首页>LeetCode·581.最短无序连续子数组·双指针
LeetCode·581.最短无序连续子数组·双指针
2022-07-28 19:47:00 【小迅想变强】
链接:https://leetcode.cn/problems/shortest-unsorted-continuous-subarray/solution/by-xun-ge-v-86rd/
来源:力扣(LeetCode)
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
题目

示例

思路
解题思路
- 解法一:排序法
因为题目需要我们找到最短无序连续子数组,最终目的是让整个数组有序,那么我们可以先将数组拷贝一份进行排序,然后使用两个指针 i 和 j 分别找到左右两端第一个不同的地方,那么 [i,j] 这一区间即是答案、即为最短无序连续子数组
- 解法二:双指针双向扫描
与解法一大体思路是一样的。我们要得到的是一个升序数组,那么我们定义两个指针 i 和 j ,分别对数组进行双向扫描。
- 对于左指针 i 而言,我们寻找最后一个降序地方;
- 对于右指针 j 而言, 我们寻找第一个降序的地方;
将第一个降序的地方和最后一个降序的地方也就是区间【j , i】,进行升序处理,是不是就得到了整体数组为升序了。
代码
- 解法一:排序法
int cmp(const void * a, const void * b)//升序子函数 { return *(int *)a - *(int *)b; } /* *int findUnsortedSubarray(int* nums, int numsSize) int findUnsortedSubarray:寻找不符合区间 int* nums:数组 int numsSize:数组长度 返回值:不符合区间长度 */ int findUnsortedSubarray(int* nums, int numsSize) { int * ans = malloc(sizeof(int) * numsSize); memcpy(ans, nums, sizeof(int) * numsSize); qsort(ans, numsSize, sizeof(int), cmp);//初始化变量 int i, j; int n = -1, m = -1; for(i = 0; i < numsSize; i++)//判断左边第一个不同点 { if(ans[i] != nums[i]) { n = i; break; } } for(j = numsSize-1; j >= 0; j--)//判断右边第一个不同点 { if(ans[j] != nums[j]) { m = j; break; } } return n == -1 ? 0 : m - n + 1; } 作者:xun-ge-v 链接:https://leetcode.cn/problems/shortest-unsorted-continuous-subarray/solution/by-xun-ge-v-86rd/ 来源:力扣(LeetCode) 著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。- 解法二:双指针双向扫描
/*
*int findUnsortedSubarray(int* nums, int numsSize)
int findUnsortedSubarray:寻找不符合区间
int* nums:数组
int numsSize:数组长度
返回值:不符合区间长度
*/
int findUnsortedSubarray(int* nums, int numsSize) {
int n = numsSize;
int maxn = INT_MIN, right = -1;
int minn = INT_MAX, left = -1;
for (int i = 0; i < n; i++) {//双向扫描
if (maxn > nums[i]) {寻找最后一个降序地方
right = i;
} else {
maxn = nums[i];
}
if (minn < nums[n - i - 1]) {寻找第一个降序的地方
left = n - i - 1;
} else {
minn = nums[n - i - 1];
}
}
return right == -1 ? 0 : right - left + 1;
}
作者:xun-ge-v
链接:https://leetcode.cn/problems/shortest-unsorted-continuous-subarray/solution/by-xun-ge-v-86rd/
来源:力扣(LeetCode)
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
时间空间复杂度
边栏推荐
- Invalid prompt object name in SQL Server
- 详细讲解C语言12(C语言系列)
- Young freshmen yearn for more open source | here comes the escape guide from open source to employment!
- First week of internship diary
- 云安全核心技术
- 30.学习Highcharts 标签旋转柱形图
- SSM use @async and create threadpooltaskexecutor thread pool
- 探讨:想要落地DevOps的话,只考虑好的PaaS容器平台就够了么?
- System integration under microservice architecture
- 将字符串指针赋值给数组[通俗易懂]
猜你喜欢

云安全核心技术

Ctfshow question making web module web11~web14

SSM-使用@Async和创建ThreadPoolTaskExecutor线程池

如何度量软件架构

Ctfshow network lost track record (1)

IJCAI2022教程 | 对话推荐系统

Why on earth is it not recommended to use select *?

MySQL 是如何归档数据的呢?

Quii Cordova plugin telerik imagepicker plug-in multi image upload out of sequence

Buuctf questions upload labs record pass-11~pass-20
随机推荐
How NPM switches Taobao source images
There have been two safety accidents in a month after listing. Is L9 ideal?
quii cordova-plugin-telerik-imagepicker插件多图上传乱序
Paging function (board)
[Topic] add two numbers
职场高薪 |「中高级测试」面试题
CVPR 2022 | in depth study of batch normalized estimation offset in network
提前布局6G赛道!紫光展锐发布《6G无界 有AI》白皮书
高举5G和AI两面旗帜:紫光展锐市场峰会火爆申城
MFC WPF WinForm (Industrial MFC or QT)
工业通讯领域的总线、协议、规范、接口、数据采集与控制系统
【Bluetooth蓝牙开发】八、BLE协议之传输层
Kubeadm搭建kubernetes集群
Analysis of critical path
ICML2022 | 时序自监督视频transformer
Another installation artifact
DLL decompile (decompile encrypted DLL)
The development of smart home industry pays close attention to edge computing and applet container technology
百度搜索符合预期,但涉及外链黑帽策略,什么原因?
Ctfshow network lost track record (2)