132模式

给定一个整数序列:a1, a2, ..., an,一个132模式的子序列 ai, aj, ak 被定义为:当 i < j < k 时,ai < ak < aj。设计一个算法,当给定有 n 个数字的序列时,验证这个序列中是否含有132模式的子序列。

注意:n 的值小于15000。

示例1:

输入: [1, 2, 3, 4]

输出: False

解释: 序列中不存在132模式的子序列。

示例 2:

输入: [3, 1, 4, 2]

输出: True

解释: 序列中有 1 个132模式的子序列: [1, 4, 2].

示例 3:

输入: [-1, 3, 2, 0]

输出: True

解释: 序列中有 3 个132模式的的子序列: [-1, 3, 2], [-1, 3, 0] 和 [-1, 2, 0].

 import java.util.Stack;

 class Solution {
public boolean find132pattern(int[] nums) {
if(nums.length<3) return false;
Stack<Integer> s=new Stack<Integer>();
int second=Integer.MIN_VALUE;
for(int i=nums.length-1;i>=0;i--){
if(nums[i]<second) return true;
while(!s.isEmpty()&&nums[i]>s.peek()){
second=s.pop();
}
s.push(nums[i]);
}
return false;
}
}

最新文章

  1. c++ eof()函数
  2. Lesson 11 One good turn deserves another
  3. Delphi下使用OpenOffice+JodConverter+SWFtools进行文件转换
  4. python学习day2--python基础
  5. hdu 4454 Stealing a Cake 三分法
  6. PL/SQL学习(五)异常处理
  7. Android Activity设置为全屏的方法
  8. Apache Hadoop 2.0.2-alpha
  9. bzoj 2669 题解(状压dp+搜索+容斥原理)
  10. Linux基础学习(9)--文件系统管理
  11. mysql如何优化插入记录速度
  12. 深入了解volatile
  13. Java反射API研究(4)——Class中的重要对象
  14. Azure 中快速搭建 FTPS 服务
  15. Map.Entry遍历集合中的元素
  16. alter table add constraint 用法
  17. PHP7-MySQLi在分页中的应用
  18. JS计算字符串的长度
  19. 第十课 go语言函数
  20. C# 设置系统环境变量

热门文章

  1. nodeis 避免回调引起的栈溢出 Maximum call stack size exceeded
  2. Codeforces Round #Pi (Div. 2) 567E President and Roads ( dfs and similar, graphs, hashing, shortest paths )
  3. [web开发] Vue + spring boot + echart 微博爬虫展示平台
  4. NHibernate使用之详细图解
  5. npm模块安装机制简介
  6. jQ实现JSON.stringify(obj)方法
  7. JS事件类型--1
  8. session添加登录次数限制
  9. 数据类型-------JavaScript
  10. C# Dictionary 的几种遍历方法,排序