You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

31 lines
658 B

package class02;
// 本题测试链接 : https://leetcode.com/problems/shortest-unsorted-continuous-subarray/
public class Code06_MinLengthForSort {
public static int findUnsortedSubarray(int[] nums) {
if (nums == null || nums.length < 2) {
return 0;
}
int N = nums.length;
int right = -1;
int max = Integer.MIN_VALUE;
for (int i = 0; i < N; i++) {
if (max > nums[i]) {
right = i;
}
max = Math.max(max, nums[i]);
}
int min = Integer.MAX_VALUE;
int left = N;
for (int i = N - 1; i >= 0; i--) {
if (min < nums[i]) {
left = i;
}
min = Math.min(min, nums[i]);
}
return Math.max(0, right - left + 1);
}
}