标签:递增 tor cto 反向 pre bar span ret subarray
题目:
解答:
l
r
1 class Solution { 2 public: 3 int findUnsortedSubarray(vector<int>& nums) 4 { 5 stack<int> st; 6 int l = nums.size() - 1; 7 int r = 0; 8 for (int i = 0; i < nums.size(); i++) 9 { 10 while (!st.empty() && nums[st.top()] > nums[i]) 11 { 12 l = min(l, st.top()); 13 st.pop(); 14 } 15 st.push(i); 16 } 17 18 st = stack<int>(); 19 for (int i = nums.size() - 1; i >= 0; i--) 20 { 21 while (!st.empty() && nums[st.top()] < nums[i]) 22 { 23 r = max(r, st.top()); 24 st.pop(); 25 } 26 st.push(i); 27 } 28 return (r > l) ? r - l + 1 : 0; 29 } 30 };
标签:递增 tor cto 反向 pre bar span ret subarray
原文地址:https://www.cnblogs.com/ocpc/p/12831462.html