456. 132 Pattern
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:
Find the subsequence s1 < s3 < s2
- Search from the end of the array, the current is the s1 candidate, because this is the first place in the array
- 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.
- If the current is smaller than the s3, which means it is definitely < all in the stack, which is the s2 candidates, return true.
- 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的更多相关文章
- 【LeetCode】456. 132 Pattern 解题报告(Python)
[LeetCode]456. 132 Pattern 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fu ...
- 【LeetCode】456. 132 Pattern
Given a sequence of n integers a1, a2, ..., an, a 132 pattern is a subsequence ai, aj, ak such that ...
- LeetCode 456. 132 Pattern
问题描述 给一组数,判断这组数中是否含有132 pattern. 132 pattern: i < j < k, 且 ai < ak < aj 第一种解法 使用栈来保存候选的子 ...
- [leetcode] 456. 132 Pattern (Medium)
对一个三个元素以上的数组,如果存在1-3-2模式的组合,则返回true. 1-3-2模式就是值的排序是i<k<j但是下标排序是i<j<k. 解法一: 硬解,利用一个变量存储是否 ...
- LC 456. 132 Pattern
Given a sequence of n integers a1, a2, ..., an, a 132 pattern is a subsequence ai, aj, ak such that ...
- 456 132 Pattern 132模式
给定一个整数序列:a1, a2, ..., an,一个132模式的子序列 ai, aj, ak 被定义为:当 i < j < k 时,ai < ak < aj.设计一个算法,当 ...
- [LeetCode] 132 Pattern 132模式
Given a sequence of n integers a1, a2, ..., an, a 132 pattern is a subsequence ai, aj, ak such that ...
- Leetcode: 132 Pattern
Given a sequence of n integers a1, a2, ..., an, a 132 pattern is a subsequence ai, aj, ak such that ...
- [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 ...
随机推荐
- jmock2.5基本教程(转)
原文:http://www.cnblogs.com/zfc2201/archive/2011/12/30/2307970.html jmock2.5基本教程 目录 第0章 概述 第1章 jmock初体 ...
- FZU 2102 Solve equation(水,进制转化)&& FZU 2111(贪心,交换使数字最小)
C Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Submit Status Pra ...
- .NET 中使用 HttpWebResponse 时 Cookie 的读取
今天把一个网站登录配置到以前写的蜘蛛程序中,发现不能成功登录.检查后才发现,那个网站在登录成功后,输出了一个特殊路径的 Cookie,由于是使用 HttpWebRequest.Cookies 来获取的 ...
- [CSS3] CSS Display Property: Block, Inline-Block, and Inline
Understanding the most common CSS display types of block, inline-block, and inline will allow you to ...
- BASE64Encoder问题类
于myeclipse于BASE64Encoder提示类不出现 对当前右击project-->Build Path--->Configure Build Path--->Java Bu ...
- LR使用Java User协议环境报错Please add the <JDK>\bin to the path and try again
看标题报错信息就知道,这是java编译及运行环境配置问题,运行LR脚本时,LR代理找不到java的JDK环境,当然,可能有人会遇到说,我在cmd窗口javac 环境是没问题的呀,是的,这就要看你的jd ...
- Flexbox属性可视化指南
Flexbox 布局(国内很多人称为弹性布局)正式的全称为 CSS Flexible Box布局模块,它是CSS3新增的一种布局模式.它可以很方便地用来改善动态或未知大小的元素的对齐,方向和顺序等等. ...
- Oracle INTERVAL DAY TO SECOND数据类型
INTERVAL DAY TO SECOND数据类型 Oracle语法: INTERVAL '{ integer | integer time_expr | time_expr }' { { DAY ...
- [Python学习笔记][第八章Python异常处理结构与程序调试]
1/30 第八章Python异常处理结构与程序调试 异常处理 try-except结构 try: try块 except Exception: except块 try-except-else结构 tr ...
- EffectiveC#1--尽可能的使用属性(property),而不是数据成员(field)
1.属性可以进行数据绑定 2.可以做数据安全校验.在对数据检测时,如果发现数据不满足条件,最好以抛出异常的形式来解决 如下代码不可取 public string Name { get { if(thi ...