581. 最短无序连续子数组

排序后,使用双指针对原数组和已排序数组进行比较

 1 class Solution {
 2     public int findUnsortedSubarray(int[] nums) {
 3         int[] sort=Arrays.copyOf(nums,nums.length);
 4         Arrays.sort(sort);
 5         int i=0,j=sort.length-1;
 6         while(i<j){
 7             if(sort[i]==nums[i]) i++;
 8             else if(sort[j]==nums[j]) j--;
 9             else break;
10         }
11         return (j-i)==0?0:(j-i+1);
12     }
13 }
原文地址:https://www.cnblogs.com/NiBosS/p/12015393.html