【LeetCode】456. 132 Pattern 解题报告(Python)
【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 日 ———— 又是一个美好的周一啦!时间真快啊!
【LeetCode】456. 132 Pattern 解题报告(Python)的更多相关文章
- 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. 解法一: 硬解,利用一个变量存储是否 ...
- 【LeetCode】120. Triangle 解题报告(Python)
[LeetCode]120. Triangle 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址htt ...
- 456. 132 Pattern
456. 132 Pattern Given an array of integers a1, a2, a3-an, judge if there exists the 132 pattern. 13 ...
- LeetCode 1 Two Sum 解题报告
LeetCode 1 Two Sum 解题报告 偶然间听见leetcode这个平台,这里面题量也不是很多200多题,打算平时有空在研究生期间就刷完,跟跟多的练习算法的人进行交流思想,一定的ACM算法积 ...
- 【LeetCode】Permutations II 解题报告
[题目] Given a collection of numbers that might contain duplicates, return all possible unique permuta ...
- 【LeetCode】Island Perimeter 解题报告
[LeetCode]Island Perimeter 解题报告 [LeetCode] https://leetcode.com/problems/island-perimeter/ Total Acc ...
- 【LeetCode】01 Matrix 解题报告
[LeetCode]01 Matrix 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/01-matrix/#/descripti ...
- 【LeetCode】Largest Number 解题报告
[LeetCode]Largest Number 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/largest-number/# ...
随机推荐
- Linux三剑客之老三grep
说明: Linux系统中grep命令是一种强大的文本搜索工具,它能使用正则表达式搜索文本,并把匹配的行打印出来.工作中我们常常用它来过滤出我们想要的数据. 格式: grep [OPTIONS] 基本参 ...
- 工作学习1-tcp自连接
运维同事反馈服务起不来.下面为了方便,写了一个demo来展示. https://gitee.com/northeast_coder/code/tree/master/case/case1_tcp_se ...
- linux 内存变量的分布
我们知道,linux通过虚拟内存管理进程的内存(进程的地址空间),而进程的地址空间分布如下 : 从进程的空间中可以看出,内存中的变量有的来自可执行elf文件,在elf文件中已经分配好存储空间,有的是在 ...
- Maven 目录结构[转载]
转载至:http://www.cnblogs.com/haippy/archive/2012/07/05/2577233.html Maven 标准目录结构 好的目录结构可以使开发人员更容易理解项目, ...
- 【STM32】使用SDIO进行SD卡读写,包含文件管理FatFs(终)-配合内存管理来遍历SD卡
[STM32]使用SDIO进行SD卡读写,包含文件管理FatFs(一)-初步认识SD卡 [STM32]使用SDIO进行SD卡读写,包含文件管理FatFs(二)-了解SD总线,命令的相关介绍 [STM3 ...
- Oracle中创建DB LINK
当用户要跨本地数据库,访问另外一个数据库表中的数据时,本地数据库中必须创建了远程数据库的dblink,通过dblink本地数据库可以像访问本地数据库一样访问远程数据库表中的数据.下面讲介绍如何在本地数 ...
- 【Linux】【Services】【SaaS】 kubeadm安装kubernetes
1. 简介 2. 环境 2.1. OS: CentOS Linux release 7.5.1804 (Core) 2.2. Ansible: 2.6.2-1.el7 2.3. docker: 2. ...
- Spring(2):依赖注入DI
依赖注入DI 当某个角色(可能是一个Java实例,调用者)需要另一个角色(另一个Java实例,被调用者)的协助时,在 传统的程序设计过程中,通常由调用者来创建被调用者的实例.但在Spring里,创建被 ...
- 2.8 GO 参数传递
简单将GO中参数传递分为三类 数字.字符.字符串等类型 结构体 方法 GO的方法本身就是地址的入口,打印一个方法输出的是这个方法的地址 func test_func(){ //0x488a30 fmt ...
- 【Linux】【Basis】Kernel
Linux Kernel: CentOS启动流程:POST --> Bootloader(BIOS, MBR) --> Kernel(initrd) --> ...