456. 132 Pattern

Given an array of integers a1, a2, a3…an, judge if there exists the 132 pattern.

132 pattern is ai < ak < aj, in which i < j < k.

Solution:

refer: https://discuss.leetcode.com/topic/67881/single-pass-c-o-n-space-and-time-solution-8-lines-with-detailed-explanation

Find the subsequence s1 < s3 < s2

  1. Search from the end of the array, the current is the s1 candidate, because this is the first place in the array
  2. If the current is smaller than the top number in the stack, push it in; If the current is larger than some of the numbers in the stack, pop the smaller ones, let s3 = the last pop, and push the current in. The stack holds the s2 candidates.
  3. If the current is smaller than the s3, which means it is definitely < all in the stack, which is the s2 candidates, return true.
  4. If the it doesn’t meet the return true condition, return false.
 public class Solution {
public boolean find132pattern(int[] nums) {
int s1, s3 = Integer.MIN_VALUE;
Stack<Integer> stack = new Stack<Integer>();
for(int i = nums.length - 1; i >= 0; i--) {
if(nums[i] < s3) return true;
else {
while(!stack.isEmpty() && nums[i] > stack.peek()) {
s3 = stack.pop();
}
}
stack.push(nums[i]);
}
return false;
}
}

456. 132 Pattern的更多相关文章

  1. 【LeetCode】456. 132 Pattern 解题报告(Python)

    [LeetCode]456. 132 Pattern 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fu ...

  2. 【LeetCode】456. 132 Pattern

    Given a sequence of n integers a1, a2, ..., an, a 132 pattern is a subsequence ai, aj, ak such that  ...

  3. LeetCode 456. 132 Pattern

    问题描述 给一组数,判断这组数中是否含有132 pattern. 132 pattern: i < j < k, 且 ai < ak < aj 第一种解法 使用栈来保存候选的子 ...

  4. [leetcode] 456. 132 Pattern (Medium)

    对一个三个元素以上的数组,如果存在1-3-2模式的组合,则返回true. 1-3-2模式就是值的排序是i<k<j但是下标排序是i<j<k. 解法一: 硬解,利用一个变量存储是否 ...

  5. LC 456. 132 Pattern

    Given a sequence of n integers a1, a2, ..., an, a 132 pattern is a subsequence ai, aj, ak such that  ...

  6. 456 132 Pattern 132模式

    给定一个整数序列:a1, a2, ..., an,一个132模式的子序列 ai, aj, ak 被定义为:当 i < j < k 时,ai < ak < aj.设计一个算法,当 ...

  7. [LeetCode] 132 Pattern 132模式

    Given a sequence of n integers a1, a2, ..., an, a 132 pattern is a subsequence ai, aj, ak such that  ...

  8. Leetcode: 132 Pattern

    Given a sequence of n integers a1, a2, ..., an, a 132 pattern is a subsequence ai, aj, ak such that ...

  9. [Swift]LeetCode456. 132模式 | 132 Pattern

    Given a sequence of n integers a1, a2, ..., an, a 132 pattern is a subsequence ai, aj, ak such that  ...

随机推荐

  1. qt model/view 架构自定义模型之QFileSystemModel

    # -*- coding: utf-8 -*- # python:2.x #QFileSystemModel """ Qt  内置了两种模型:QStandardItemM ...

  2. Js操作Select

    jquery操作select(取值,设置选中) 每一次操作select的时候,总是要出来翻一下资料,不如自己总结一下,以后就翻这里了. 比如<select class="selecto ...

  3. [转]XNOR-Net ImageNet Classification Using Binary Convolutional Neural Networks

    感谢: XNOR-Net ImageNet Classification Using Binary Convolutional Neural Networks XNOR-Net ImageNet Cl ...

  4. 33c3-pwn350-tea

    TEA 感觉这个题目出得很不错.先运行程序了解基本功能,程序可以读取对系统上存在的文件的内容,如果文件不存在的话,直接退出. 使用IDA打开后,发现父进程通过clone api克隆出一个子进程,主要的 ...

  5. UIwebView的html字符串高度计算

    ) { webView = [[UIWebView alloc]initWithFrame:CGRectMake(, , DEVW-, webviewH)]; webView.delegate = s ...

  6. CSS属性值定义语法中的符号说名

    我们通常看到一个CSS语法,总是有很多符号在其中,这些符号是什么鬼呢,且看下面道来 这些符号可以大致分为2类:分组类与数量类. 1.分组类,就是分成一堆一堆啦: 符号 名称 描述 示例   并置 各部 ...

  7. 豆瓣FM duilib版

    最近duilib项目被复制到了github上,仿佛又多了些活力.想要总结以前的项目的同时因为很喜欢豆瓣的FM,所以打算做个duilib版本豆瓣FM. 在网上了看到了很多豆瓣的不同版本,node.js. ...

  8. 【solr专题之二】配置文件:solr.xml solrConfig.xml schema.xml

    1.关于默认搜索域 If you are using the Lucene query parser, queries that don't specify a field name will use ...

  9. jQuery对checkbox的各种操作

    //注意: 操作checkbox的checked,disabled属性时jquery1.6以前版本用attr,1.6以上(包含)建议用prop //1.根据id获取checkbox $("# ...

  10. Scrapy学习系列(一):网页元素查询CSS Selector和XPath Selector

    这篇文章主要介绍创建一个简单的spider,顺便介绍一下对网页元素的选取方式(css selector, xpath selector). 第一步:创建spider工程 打开命令行运行以下命令: sc ...