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

标签(空格分隔): LeetCode

作者: 负雪明烛
id: fuxuemingzhu
个人博客: http://fuxuemingzhu.cn/


题目地址:https://leetcode.com/problems/132-pattern/description/

题目描述:

Given a sequence of n integers a1, a2, …, an, a 132 pattern is a subsequence ai, aj, ak such that i < j < k and ai < ak < aj. Design an algorithm that takes a list of n numbers as input and checks whether there is a 132 pattern in the list.

Note: n will be less than 15,000.

Example 1:

Input: [1, 2, 3, 4]

Output: False

Explanation: There is no 132 pattern in the sequence.

Example 2:

Input: [3, 1, 4, 2]

Output: True

Explanation: There is a 132 pattern in the sequence: [1, 4, 2].

Example 3:

Input: [-1, 3, 2, 0]

Output: True

Explanation: There are three 132 patterns in the sequence: [-1, 3, 2], [-1, 3, 0] and [-1, 2, 0].

题目大意

在一个序列中找到132的模式,就是第一个数小于第二第三个数,且第三个数小于第二个数。

解题方法

这个题我感觉好难,想不明白。摘抄http://www.cnblogs.com/grandyang/p/6081984.html解法如下:

下面这种方法利用来栈来做,既简洁又高效,思路是我们维护一个栈和一个变量third,其中third就是第三个数字,也是pattern
132中的2,栈里面按顺序放所有大于third的数字,也是pattern
132中的3,那么我们在遍历的时候,如果当前数字小于third,即pattern
132中的1找到了,我们直接返回true即可,因为已经找到了,注意我们应该从后往前遍历数组。
如果当前数字大于栈顶元素,那么我们按顺序将栈顶数字取出,赋值给third,然后将该数字压入栈,这样保证了栈里的元素仍然都是大于third的,我们想要的顺序依旧存在,进一步来说,栈里存放的都是可以维持second > third的second值,其中的任何一个值都是大于当前的third值,如果有更大的值进来,那就等于形成了一个更优的second > third的这样一个组合,并且这时弹出的third值比以前的third值更大,为什么要保证third值更大,因为这样才可以更容易的满足当前的值first比third值小这个条件。

代码如下:

class Solution:
def find132pattern(self, nums):
"""
:type nums: List[int]
:rtype: bool
"""
if len(nums) <=2:
return False
third = float('-inf')
stack = []
for i in range(len(nums)-1, -1, -1):
if nums[i] < third:
return True
else:
while stack and stack[-1] < nums[i]:
third = stack.pop()
stack.append(nums[i])
return False

日期

2018 年 8 月 20 日 ———— 又是一个美好的周一啦!时间真快啊!

最新文章

  1. redux的中间层 --reactjs学习
  2. TableView 隐藏多余的分割线
  3. InnoDB和MyISAM(转)
  4. [python]python元类
  5. C++在使用Qt中SLOT宏需要注意的一个小细节
  6. Java中RMI框架
  7. Android SDK的下载和安装
  8. 了不起的分支和循环02 - 零基础入门学习Python008
  9. 线程UI同步
  10. JavaScript 代码简洁之道
  11. pheatmap, gplots heatmap.2和ggplot2 geom_tile实现数据聚类和热图plot
  12. oracle service name sid , 用户 和 表空间
  13. 整理python小爬虫
  14. 说说Android6.0动态申请权限的那些坑
  15. Java中A instanceof B是什么意思?
  16. VMWare Station 问题汇总
  17. hbase与hive集成:hive读取hbase中数据
  18. map reduce 用法 str处理lower() capitalize()
  19. 【CF938G】Shortest Path Queries(线段树分治,并查集,线性基)
  20. [LOJ6198]谢特

热门文章

  1. phpexcel 另存Excel文件方式
  2. 截取字符串、拼接字符串【c#】
  3. C# CheckBoxList-DropDownList回显、筛选回显
  4. 【原创】基于RPA的软件功能自动化测试
  5. 日常Java 2021/9/19
  6. 零基础学习java------day16-----文件,递归,IO流(字节流读写数据)
  7. STM32 部分重映射和完全重映射(查看数据手册)
  8. C++福尔摩斯的约会
  9. Output of C++ Program | Set 16
  10. Mysql的表级锁