Given an integer array, you need to find one continuous subarray that if you only sort this subarray in ascending order, then the whole array will be sorted in ascending order, too.
You need to find the shortest such subarray and output its length.
Example 1:
Input: [2, 6, 4, 8, 10, 9, 15]
Output: 5
Explanation: You need to sort [6, 4, 8, 10, 9] in ascending order to make the whole array sorted in ascending order.
Note:
Then length of the input array is in range [1, 10,000].
The input array may contain duplicates, so ascending order here means <=.
难度:简单
class Solution {
public:
int findUnsortedSubarray(vector<int>& nums) {
int len =nums.size();
int num_min=INT_MAX;
int num_max=INT_MIN;
//一次扫描,同时求出无序子数组中最小元素和最大元素
for(int i=0;i<len-1;i++){
if(nums[i+1]<nums[i]){
num_min=min(nums[i+1],num_min);
num_max=max(nums[i],num_max);
}
}
//确定最小元素的正确位置
int l=0;
for(int i=0;i<len-1;i++){
if(nums[i]>num_min) {
l=i;
break;
}
}
//确定最大元素的正确位置
int r=0;
for(int i=len-1;i>-1;i--){
if(nums[i]<num_max){
r=i;
break;
}
}
//返回结果这里需要特殊处理一下数组已是升序如[1,2,3,4],l=0,r=0的特殊情况
if(!r&&!l) return 0;
else return r-l+1;
}
};
ascending | 递增 |
---|