当前位置:网站首页>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)
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
时间空间复杂度
边栏推荐
- Attribute based encryption simulation and code implementation (cp-abe) paper: ciphertext policy attribute based encryption
- How to measure software architecture
- Library borrowing system "suggested collection"
- (PMIC)全、半桥驱动器CSD95481RWJ PDF 规格
- 8、 QoS queue scheduling and message discarding
- Automatic filling of spare parts at mobile end
- Top level "redis notes", cache avalanche + breakdown + penetration + cluster + distributed lock, Nb
- Reading and writing basic data types in protobuf
- Adventures of little mouse: behind the scenes gags of moss 2
- First week of internship diary
猜你喜欢

Redis cache avalanche, cache penetration, cache breakdown

ctfshow 网络迷踪做题记录(2)

BUUCTF做题Upload-Labs记录pass-01~pass-10

35 道 MySQL 面试必问题图解,这样也太好理解了吧

上市1个月接连发生两起安全事故,理想L9还理想吗?

MySQL 是如何归档数据的呢?

Bus, protocol, specification, interface, data acquisition and control system in industrial communication field

Top level "redis notes", cache avalanche + breakdown + penetration + cluster + distributed lock, Nb

证券企业基于容器化 PaaS 平台的 DevOps 规划建设 29 个典型问题总结

Coding with these 16 naming rules can save you more than half of your comments!
随机推荐
How to measure software architecture
uniapp的进度条自定义
SQL Server 数据库之备份和恢复数据库
Buuctf questions upload labs record pass-01~pass-10
How NPM switches Taobao source images
Study and use of cobalt strike
【Bluetooth蓝牙开发】八、BLE协议之传输层
牛客打开摄像头几秒后画面消失 | 相机打开画面一闪一闪
Achieve waterfall effect
Why on earth is it not recommended to use select *?
不用Swagger,那我用啥?
Using El date picker to report errors in sub components
Uniapp progress bar customization
职场高薪 |「中高级测试」面试题
探讨:想要落地DevOps的话,只考虑好的PaaS容器平台就够了么?
苹果M1处理器详解:性能及能效成倍提升,Intel酷睿i9也不是对手!
First week of internship diary
4.1 various calling methods of member
将字符串指针赋值给数组[通俗易懂]
MFC WPF WinForm (Industrial MFC or QT)