#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 发布