LeetCode456(2021.3.24)

LC456. 132 模式

class Solution {
    public boolean find132pattern(int[] nums) {
        Deque<Integer> stack = new LinkedList<Integer>(); //单调递增栈
        stack.push(nums[nums.length - 1]);
        int k = Integer.MIN_VALUE;
        for (int i = nums.length - 2; i >= 0; --i) {
            if (nums[i] < k) {
                return true;
            }
            while (!stack.isEmpty() && nums[i] > stack.peek()) {
                k = stack.pop();
            }
            if (nums[i] > k) {
                stack.push(nums[i]);
            }
        }
        return false;
    }
}

发表评论