给定一个整数数组,你需要寻找一个连续的子数组,如果对这个子数组进行升序排序,那么整个数组都会变为升序排序。
你找到的子数组应是最短的,请输出它的长度。
示例 1:
输入: [2, 6, 4, 8, 10, 9, 15] 输出: 5 解释: 你只需要对 [6, 4, 8, 10, 9] 进行升序排序,那么整个表都会变为升序排序。
说明 :
- 输入的数组长度范围在 [1, 10,000]。
- 输入的数组可能包含重复元素 ,所以升序的意思是<=。
class Solution {
public:
int findUnsortedSubarray(vector<int>& nums)
{
int len = nums.size();
if(len <= 1)
return 0;
vector<int> temp = nums;
sort(temp.begin(), temp.end());
int p1 = 0, p2 = len - 1;
for(int i = 0; i < len; i++)
{
if(nums[i] != temp[p1])
break;
p1++;
}
for(int i = len - 1; i >= 0; i--)
{
if(nums[i] != temp[p2])
break;
p2--;
}
return p2 - p1 <= 0? 0 : p2 - p1 + 1;
/*
vector<int> res;
for(int i = p1; i <= p2; i++)
{
res.push_back(nums[i]);
}
return res;
*/
}
};