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. poj 2305(指定进制,大数取模)

    题意:输入一个进制b,在输入两个基于b进制的大整数 x,y ,求x%y的b进制结果. http://162.105.81.212/JudgeOnline/problem?id=2305 函数: Str ...

  2. PHP文件目录copy

    /**(2) PHP文件目录copy @param string $dirsrc 原目录名称字符串 @param string $dirto 目标目录名称字符串 */ function copyDir ...

  3. Unity四种路径总结

    四种路径的权限:                                            Application.dataPath 包含游戏数据文件夹的路径(只读) Applicatio ...

  4. [CSS] CSS Transitions: Delays and Multiple Properties

    <!DOCTYPE html> <html> <head> <script src="https://code.jquery.com/jquery- ...

  5. QtXlsxWriter

    Code Issues26 Pull requests2   Pulse Graphs HTTPS clone URL You can clone with HTTPS orSubversion. C ...

  6. [Spring入门学习笔记][静态资源]

    遗留问题 在上一节课的作业中,我们一定遇到了一点问题——虽然将页面内容正确的返回给了浏览器,但是浏览器显示的样式却是不正确的,这是因为在HTML的\标签中我们这样引入了CSS资源: <link ...

  7. linux 系统下配置安装 java jdk 图文流程

    先查看一下系统版本,本例采用的操作系统是CentOS 6.5: 如果你是初装之后的操作系统,那么有可能wget这个组件是不存在的,所以你要安装一下它,这样才可以让你从网上down下你要的安装包: 上面 ...

  8. EffectiveC#01--避免返回内部类对象的引用

    此篇是对00中第3点的再一次阐述. 1.如果一个属性返回一个引用类型,那么调用者就可以访问这个对象的公共成员,也包括修改这些属性的状态. public class MyBusinessObject { ...

  9. App签名--- Android

    步骤: 下面就Next即可

  10. Android ListView使用(非原创)

    1.ListView:它以列表的形式展示具体要显示的内容,并且能够根据数据的长度自适应屏幕显示 <LinearLayout xmlns:android="http://schemas. ...