581. Shortest Unsorted Continuous Subarray
程序员文章站
2024-02-24 14:47:40
...
Solution:
思路:
Time Complexity: O(N) Space Complexity: O(N)
Solution Code:
class Solution {
public int findUnsortedSubarray(int[] A) {
int n = A.length;
int begin = -1, end = -2;
int min = A[n - 1], max = A[0];
for (int i = 1; i < n; i++) {
max = Math.max(max, A[i]);
min = Math.min(min, A[n - 1 - i]);
if (A[i] < max) end = i;
if (A[n - 1 - i] > min) begin = n - 1 - i;
}
return end - begin + 1;
}
}
推荐阅读
-
581. Shortest Unsorted Continuous Subarray
-
581. Shortest Unsorted Continuous Subarray
-
# 189. Rotate Array # 581. Shortest Unsorted Continuous Subarray
-
LeetCode 581. Shortest Unsorted Contimuous Subarray
-
581. Shortest Unsorted Continuous Subarray
-
leetcode(581):Shortest Unsorted Continuous Subarray
-
[LeetCode]581. Shortest Unsorted Continuous Subarray
-
【时间复杂度优化】LeetCode 581. Shortest Unsorted Continuous Subarray
-
Leetcode 581. Shortest Unsorted Continuous Subarray