当前位置:网站首页>Leetcode · 581. shortest unordered continuous subarray · double pointer
Leetcode · 581. shortest unordered continuous subarray · double pointer
2022-07-28 21:55:00 【Xiao Xun wants to be strong】
link :https://leetcode.cn/problems/shortest-unsorted-continuous-subarray/solution/by-xun-ge-v-86rd/
source : Power button (LeetCode)
The copyright belongs to the author . Commercial reprint please contact the author for authorization , Non-commercial reprint please indicate the source .
subject
Example
Ideas
Their thinking
- Solution 1 : Sequencing
Because the problem requires us to find the shortest unordered continuous subarray , The ultimate goal is to make the entire array orderly , Then we can first copy the array and sort it , Then use two pointers i and j Find the first difference between the left and right ends , that [i,j] This interval is the answer 、 That is, the shortest unordered continuous subarray
- Solution 2 : Double pointer bidirectional scanning
The general idea of solution is the same . What we want is an ascending array , Then we define two pointers i and j , Scan the array in both directions .
- For the left pointer i for , We look for the last place in descending order ;
- For the right pointer j for , We look for the first place in descending order ;
The first place in descending order and the last place in descending order are intervals 【j , i】, Perform ascending processing , Does it mean that the whole array is in ascending order .
Code
- Solution 1 : Sequencing
int cmp(const void * a, const void * b)// Ascending subfunction { return *(int *)a - *(int *)b; } /* *int findUnsortedSubarray(int* nums, int numsSize) int findUnsortedSubarray: Find the non-conforming interval int* nums: Array int numsSize: The length of the array Return value : It does not conform to the interval length */ int findUnsortedSubarray(int* nums, int numsSize) { int * ans = malloc(sizeof(int) * numsSize); memcpy(ans, nums, sizeof(int) * numsSize); qsort(ans, numsSize, sizeof(int), cmp);// Initialize variable int i, j; int n = -1, m = -1; for(i = 0; i < numsSize; i++)// Judge the first difference on the left { if(ans[i] != nums[i]) { n = i; break; } } for(j = numsSize-1; j >= 0; j--)// Judge the first difference on the right { if(ans[j] != nums[j]) { m = j; break; } } return n == -1 ? 0 : m - n + 1; } author :xun-ge-v link :https://leetcode.cn/problems/shortest-unsorted-continuous-subarray/solution/by-xun-ge-v-86rd/ source : Power button (LeetCode) The copyright belongs to the author . Commercial reprint please contact the author for authorization , Non-commercial reprint please indicate the source .
- Solution 2 : Double pointer bidirectional scanning
/*
*int findUnsortedSubarray(int* nums, int numsSize)
int findUnsortedSubarray: Find the non-conforming interval
int* nums: Array
int numsSize: The length of the array
Return value : It does not conform to the interval length
*/
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++) {// Bidirectional scanning
if (maxn > nums[i]) { Find the last place in descending order
right = i;
} else {
maxn = nums[i];
}
if (minn < nums[n - i - 1]) { Find the first place in descending order
left = n - i - 1;
} else {
minn = nums[n - i - 1];
}
}
return right == -1 ? 0 : right - left + 1;
}
author :xun-ge-v
link :https://leetcode.cn/problems/shortest-unsorted-continuous-subarray/solution/by-xun-ge-v-86rd/
source : Power button (LeetCode)
The copyright belongs to the author . Commercial reprint please contact the author for authorization , Non-commercial reprint please indicate the source .
Time and space complexity
边栏推荐
- 详解visual studio 2015在局域网中远程调试程序
- 【英雄哥七月集训】第 28天:动态规划
- 微星宝安工厂失火!官方回应:无人员受伤,产线不受影响!
- 顺序表的实现
- Storage and steps of phospholipid coupled antibody / protein Kit
- 株洲市九方中学开展防溺水、消防安全教育培训活动
- Miscellaneous records of powersploit, evaluation, weevery and other tools in Kali
- 日志瘦身神操作:从5G优化到1G到底是怎么做到的!(荣耀典藏版)
- 入行4年,跳槽2次,我摸透了软件测试这一行~
- 这种动态规划你见过吗——状态机动态规划之股票问题(下)
猜你喜欢
蚂蚁集团境外站点 Seata 实践与探索
Mesh data generation function meshgrid
Construction of Chinese traditional embroidery classification model based on xception TD
入行4年,跳槽2次,我摸透了软件测试这一行~
如何高效、精准地进行图片搜索?看看轻量化视觉预训练模型
Storage and steps of phospholipid coupled antibody / protein Kit
Implementation of sequence table
纳米金偶联抗体/蛋白试剂盒(20nm,1mg/100μg/500 μg偶联量)的制备
Is it necessary to calibrate the fluke dtx-1800 test accuracy?
[Bluetooth Bluetooth development] VIII. Transmission layer of ble protocol
随机推荐
Two global variables__ Dirname and__ Further introduction to common functions of filename and FS modules
An end-to-end aspect level emotion analysis method for government app reviews based on brnn
小程序开发需要什么技术
Which brand is the best and most cost-effective open headset
【英雄哥七月集训】第 28天:动态规划
The Swedish court lifted the 5g spectrum auction ban on Huawei and ZTE
Huawei releases the first electric drive system driveone: charging for 10 minutes, endurance of 200km
With the help of domestic chip manufacturers, the shipment of white brand TWS headphones has reached 600million in 2020
国产芯片厂商助力,2020年白牌TWS耳机出货已达6亿部
Cy3/Cy5/Cy5.5/Cy7荧光标记抗体/蛋白试剂盒(10~100mg标记量)
提前布局6G赛道!紫光展锐发布《6G无界 有AI》白皮书
Leetcode 19. delete the penultimate node of the linked list [knowledge points: speed pointer, recursion, stack]
RHCSA第一天
Data interpolation -- normalize data of different magnitude
瑞典法院取消对华为和中兴的5G频谱拍卖禁令
Nano gold coupled antibody / protein Kit (20nm, 1mg/100 μ g/500 μ G coupling amount) preparation
针对下一代Chromebook,联发科推出新款芯片组MT8192和MT8195
Apifox:满足你对 Api 的所有幻想
Leetcode 142. circular linked list II [knowledge points: speed pointer, hash table]
中国科学家首次用DNA构造卷积人工神经网络,可完成32类分子模式识别任务,或用于生物标志物信号分析和诊断