#include
#include
#include
using namespace std;
class Soltution {
public:int findUnsortedSubarry(vector& nums) {
int len = size(nums);
vectortmp(nums.begin(), nums.end());
sort(tmp.begin(), tmp.end());
int i = 0;
int j = 0;
for (i = 0;i < len;i++)
{
if (nums[i] != tmp[i])
break;
}
for (j = len - 1;j >= 0;j–)
{
if (nums[j] != tmp[j])
break;
}
if (j > i)
return j - i + 1;
return 0;
}
};
int main()
{
int n = 0;
cout << "please input the array’s length: ";
cin >> n;
vectorarr(n);
cout << "please input the array’s elemt: ";
for (int& a : arr)
cin >> a;
Soltution s;
cout << s.findUnsortedSubarry(arr);
return 0;
}
对数组最短的序列进行排序
最新推荐文章于 2022-09-27 00:26:56 发布
本文介绍了一种算法,用于找出一个整数数组中未排序部分的最短长度。通过复制并排序原始数组,然后比较两个数组以找到不匹配元素的位置,从而确定未排序子数组的边界。
817

被折叠的 条评论
为什么被折叠?



