int Mycmp(const void* a, const void* b)
{
return *(int*)a - *(int*)b;
}
int findUnsortedSubarray(int* nums, int numsSize){
int* arr = (int*)calloc(numsSize, sizeof(int));
memcpy(arr, nums, numsSize*sizeof(int));
qsort(arr, numsSize, sizeof(int), Mycmp);
int head = -1;
int tail = numsSize;
for (int i = 0; i < numsSize; i++)
{
if (head == -1 && nums[i] != arr[i])head = i;
if (tail == numsSize && nums[numsSize - 1 - i] != arr[numsSize - 1 - i])tail = numsSize - 1 - i;
if (head != -1 && tail != numsSize) return tail-head+1;
}
return 0;
}
原文:https://www.cnblogs.com/ganxiang/p/13619633.html