单调栈解决132模式
这道题实在是想不出来,看了题解区的大佬的题解,恍然大悟。
贴下大佬的代码:
class Solution {
public boolean find132pattern(int[] nums) {
int len = nums.length;
int last = Integer.MIN_VALUE;
Stack<Integer> stack = new Stack<>();
if(len < 3){
return false;
}
for(int i = len - 1; i >= 0; i--){
if(nums[i] < last){
return true;
}
while(!stack.isEmpty() && stack.peek() < nums[i]){
last = stack.pop();
}
stack.push(nums[i]);
}
return false;
}
}字节算法题解 文章被收录于专栏
最近在刷字节的题库!! 春招给我冲!!!
查看14道真题和解析

巨人网络公司福利 91人发布