方法一:暴力解法
- 要归纳出:
- 找到每个数左边第 1 个严格大于它的值的下标(构成逆序关系);
- 找到每个数右边第 1 个严格小于它的值的下标(也构成逆序关系)。
二者之差就是题目要找的「最短无序连续子数组」。
Java 代码:
public class Solution {
public int findUnsortedSubarray(int[] nums) {
int len = nums.length;
if (len < 2) {
return 0;
}
// 从左到右找出最后一个单调不减的下标
// 从右到左找出最后一个单调不增的下标
int max = nums[0];
int right = 0;
for (int i = 1; i < len; i++) {
max = Math.max(max, nums[i]);
if (nums[i] < max) {
right = i;
}
}
// System.out.println(right);
int left = len - 1;
int min = nums[len - 1];
for (int i = len - 2; i >= 0; i--) {
min = Math.min(min, nums[i]);
if (nums[i] > min) {
left = i;
}
}
// System.out.println(left);
if (right > left) {
return right - left + 1;
}
return 0;
}
public static void main(String[] args) {
Solution solution = new Solution();
int[] nums = {
2, 6, 4, 8, 10, 9, 15};
int res = solution.findUnsortedSubarray(nums);
System.out.println(res);
}
}
方法二:栈(单调栈)
Java 代码:
import java.util.ArrayDeque;
import java.util.Deque;
public class Solution {
public int findUnsortedSubarray(int[] nums) {
int len = nums.length;
if (len < 2) {
return 0;
}
int leftBound = len - 1;
int rightBound = 0;
Deque<Integer> stack = new ArrayDeque<>(len);
for (int i = 0; i < len; i++) {
// 这里的 while 不要忘记
while (!stack.isEmpty() && nums[i] < nums[stack.peekLast()]) {
leftBound = Math.min(leftBound, stack.removeLast());
}
stack.addLast(i);
}
stack.clear();
for (int i = len - 1; i >= 0; i--) {
while (!stack.isEmpty() && nums[i] > nums[stack.peekLast()]) {
rightBound = Math.max(rightBound, stack.removeLast());
}
stack.addLast(i);
}
if (rightBound > leftBound) {
return rightBound - leftBound + 1;
}
return 0;
}
}