Leetcode 456. 132 Pattern

版权声明:博客文章都是作者辛苦整理的,转载请注明出处,谢谢! https://blog.csdn.net/Quincuntial/article/details/83659707

文章作者:Tyan
博客:noahsnail.com  |  CSDN  |  简书

1. Description

132 Pattern

2. Solution

  • Version 1
class Solution {
public:
    bool find132pattern(vector<int>& nums) {
        int size = nums.size();
        if(size == 0) {
            return false;
        }
        int min = nums[0];
        for(int j = 1; j < size - 1; j++) {
            if(nums[j] > min) {
                for(int k = j + 1; k < size; k++) {
                    if(nums[j] > nums[k] && min < nums[k]) {
                        return true;
                    }
                }
            }
            else {
                min = nums[j];
            }
        }
        return false;
    }
};
  • Version 2
class Solution {
public:
    bool find132pattern(vector<int>& nums) {
        int third = INT_MIN;
        stack<int> s;
        for(int i = nums.size() - 1; i >= 0; i--) {
            if(nums[i] < third) {
                return true;
            } 
            while(!s.empty() && nums[i] > s.top()) {
                third = s.top();
                s.pop();
            }
            s.push(nums[i]);
        }
        return false;
    }
};

Reference

  1. https://leetcode.com/problems/132-pattern/description/
阅读更多

扫码向博主提问

SnailTyan

博客专家

非学,无以致疑;非问,无以广识
  • 擅长领域:
  • 深度学习
  • PyTorch
  • OCR
  • Docker
  • Caffe
去开通我的Chat快问
换一批

没有更多推荐了,返回首页