Leetcode 456.132模式
2024-08-28 13:31:10
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;
}
}
最新文章
- c++ eof()函数
- Lesson 11 One good turn deserves another
- Delphi下使用OpenOffice+JodConverter+SWFtools进行文件转换
- python学习day2--python基础
- hdu 4454 Stealing a Cake 三分法
- PL/SQL学习(五)异常处理
- Android Activity设置为全屏的方法
- Apache Hadoop 2.0.2-alpha
- bzoj 2669 题解(状压dp+搜索+容斥原理)
- Linux基础学习(9)--文件系统管理
- mysql如何优化插入记录速度
- 深入了解volatile
- Java反射API研究(4)——Class中的重要对象
- Azure 中快速搭建 FTPS 服务
- Map.Entry遍历集合中的元素
- alter table add constraint 用法
- PHP7-MySQLi在分页中的应用
- JS计算字符串的长度
- 第十课 go语言函数
- C# 设置系统环境变量
热门文章
- nodeis 避免回调引起的栈溢出 Maximum call stack size exceeded
- Codeforces Round #Pi (Div. 2) 567E 	President and Roads ( dfs and similar, graphs, hashing, shortest paths )
- [web开发] Vue + spring boot + echart 微博爬虫展示平台
- NHibernate使用之详细图解
- npm模块安装机制简介
- jQ实现JSON.stringify(obj)方法
- JS事件类型--1
- session添加登录次数限制
- 数据类型-------JavaScript
- C# Dictionary 的几种遍历方法,排序