当前位置:网站首页>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
边栏推荐
- What technology is needed for applet development
- B+ tree height calculation of MySQL
- 瑞典法院取消对华为和中兴的5G频谱拍卖禁令
- Matlab|基础知识总结一
- 不用Swagger,那我用啥?
- 8、 QoS queue scheduling and message discarding
- Meeting notice of OA project (Query & whether to attend the meeting & feedback details)
- [geek challenge 2019] secret file & file contains common pseudo protocols and gestures
- Divide and conquer, upload large files in pieces
- msfvenom制作主控与被控端
猜你喜欢

两个全局变量__dirname和__filename 、fs模块常用功能进一步介绍

Construction of Chinese traditional embroidery classification model based on xception TD

使用Mock技术帮助提升测试效率的小tips,你知道几个?

开放式耳机哪个品牌好、性价比最高的开放式耳机排名

RHCSA第一天

Data interpolation -- normalize data of different magnitude

基于复杂网络的大群体应急决策专家意见与信任信息融合方法及应用

Zhuzhou Jiufang middle school carried out drowning prevention and fire safety education and training activities

Talk about row storage and column storage of database

搞事摸鱼一天有一天
随机推荐
Leetcode linked list problem -- 142. circular linked list II (learn the linked list by one question and one article)
数据库读写分离目的是做什么[通俗易懂]
聊一聊数据库的行存与列存
Nano gold coupled antibody / protein Kit (20nm, 1mg/100 μ g/500 μ G coupling amount) preparation
Leetcode interview question 02.07. Linked list intersection [knowledge points: Double pointers, stack]
中国科学家首次用DNA构造卷积人工神经网络,可完成32类分子模式识别任务,或用于生物标志物信号分析和诊断
Divide and conquer, upload large files in pieces
Adventures of little mouse: behind the scenes gags of moss 2
比UUID更快更安全NanoID到底是怎么实现的?(荣耀典藏版)
软考 --- 数据库(3)数据操作
With the help of domestic chip manufacturers, the shipment of white brand TWS headphones has reached 600million in 2020
数据插值——对不同量级的数据进行归一化
高举5G和AI两面旗帜:紫光展锐市场峰会火爆申城
Is it necessary to calibrate the fluke dtx-1800 test accuracy?
Priced at 1.15 billion yuan, 1206 pieces of equipment were injected into the joint venture! Sk Hynix grabs the mainland wafer foundry market!
网格数据生成函数meshgrid
使用Mock技术帮助提升测试效率的小tips,你知道几个?
Matlab|基础知识总结一
Implementation of sequence table
苹果M1处理器详解:性能及能效成倍提升,Intel酷睿i9也不是对手!