class Solution {
public:
bool find132pattern(vector<int>& nums)
{
if(nums.size() <= 2)
return false;
stack<int> three;
int two = INT_MIN;
for(int i = nums.size() - 1;i >= 0;i--)
{
if(nums[i] < two)
{
return true;
}
while(!three.empty() && nums[i] > three.top())
{
two = max(two,three.top());
three.pop();
}
three.push(nums[i]);
}
return false;
}
};
算法思路:
想了挺久没想出来不超时的做法==,最后看了题解写的;
1.如果nums的大小小于等于2,必然构不成132模式;
2.从后往前进行遍历寻找,因为后面的更有规律,从后面往前遍历一定有32形式,只需要判断是否小于2即可,如果从前往后进行遍历则不可以这样判断。
3.利用一个栈存储最大的3.而two取栈中剩余部分与他相比较的最大值。