import java.util.*;
public class Solution {
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
*
* @param nums int整型一维数组
* @return int整型
*/
public int findUnsortedSubarray(int[] nums) {
// write code here
boolean[] leftMax = new boolean[nums.length];
boolean[] rightMin = new boolean[nums.length];
leftMax[0] = true;
rightMin[nums.length - 1] = true;
int lm = nums[0];
int rm = nums[nums.length - 1];
for (int i = 1; i < nums.length; i++) {
if (nums[i] >= lm) {
leftMax[i] = true;
lm = nums[i];
}
}
for (int i = nums.length - 2; i > -1; i--) {
if (nums[i] <= rm) {
rightMin[i] = true;
rm = nums[i];
}
}
int start = -1;
int end = -1;
for (int i = 0; i < nums.length; i++) {
if (!(leftMax[i] && rightMin[i])) {
start = i;
break;
}
}
for (int i = nums.length - 1; i > -1; i--) {
if (!(leftMax[i] && rightMin[i])) {
end = i;
break;
}
}
if (start == -1 && end == -1) {
return 0;
}
return end - start + 1;
}
}