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模式,即i<j<k时,是否有a[i]<a[k]<a[j]
这个题我是没有思路的,所以只有使用暴力了,但是还是提交了很多次才ac,果然昏睡状态下是不适合做题的。
暴力破解思路:
  每循环到一个值a[j]的时候,找出这个值前面的最小值a[i],同时找出这个值后面的符合条件的值a[k]
ps:这个题让人很受伤,1750ms
bool find132pattern(int* nums, int numsSize) {
int i;
int p=0,q=0;
int lmin=nums[0],flag;
for(i=1;i<numsSize-1;i++)
{
flag=0;
for(;p<i;p++)      //这里要保存p和lmin的状态,否则会超时不能ac,都是泪
if(lmin>nums[p])
lmin=nums[p];
for(q=numsSize-1;q>i;q--)
if(lmin<nums[q]&&nums[q]<nums[i])
{
flag=1;
break;
}
if(flag&&nums[q]<nums[i])
return true;
}
return false;
}

看到了另外一种解法,9ms

思路:

  逆遍历整个数组,维护一个已遍历的第二大的数,维护一个存放最大的数的数组,当出现比第二大的数小的数的时候,返回True

C代码:

 bool find132pattern(int* nums, int numsSize) {
int second=INT_MIN;
int tmp[numsSize];
int i,len=;
for(i=numsSize-;i>=;i--)
{
if(nums[i]<second)
return true;            //当出现比第二大的数小的时候,返回True
while(len>&&nums[i]>tmp[len-])
second=tmp[--len];         //获得比新出现的最大数小的第二大的数
tmp[len++]=nums[i];
}
return false;
}

Python代码:

 class Solution(object):
def find132pattern(self, nums):
"""
:type nums: List[int]
:rtype: bool
"""
second = -10000000000000 #不知道怎么设置Python中的最小整形值
st = []
for num in nums[::-1]:
if num<second:
return True
while st and num>st[-1]:
second=st.pop()
st.append(num)
return False

【LeetCode】456. 132 Pattern的更多相关文章

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

    [LeetCode]456. 132 Pattern 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fu ...

  2. 【LeetCode】290. Word Pattern 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...

  3. 【leetcode】290. Word Pattern

    problem 290. Word Pattern 多理解理解题意!!! 不过博主还是不理解,应该比较的是单词的首字母和pattern的顺序是否一致.疑惑!知道的可以分享一下下哈- 之前理解有误,应该 ...

  4. 456. 132 Pattern

    456. 132 Pattern Given an array of integers a1, a2, a3-an, judge if there exists the 132 pattern. 13 ...

  5. 【leetcode】44. Wildcard Matching

    题目如下: 解题思路:本题和[leetcode]97. Interleaving String非常相似,同样可以采用动态规划的方法.记dp[i][j] = 1或者0 表示pattern[0:i]是否匹 ...

  6. 【LeetCode】385. Mini Parser 解题报告(Python)

    [LeetCode]385. Mini Parser 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode.com/problems/mini-parser/ ...

  7. 【LeetCode】Minimum Depth of Binary Tree 二叉树的最小深度 java

    [LeetCode]Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum dept ...

  8. 【Leetcode】Pascal&#39;s Triangle II

    Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3, Return [1,3 ...

  9. 53. Maximum Subarray【leetcode】

    53. Maximum Subarray[leetcode] Find the contiguous subarray within an array (containing at least one ...

随机推荐

  1. Django 同步数据库命令syncdb,makemigrations,migrate

    syncdb只支持新增模型到数据库中 2条命令 你将使用2条命令进行迁移和操纵数据库schema: migrate,用来使迁移生效,以及未生效时报告它们的状态. makemigrations,用来根据 ...

  2. Qt 富文本处理

    富文本处理 所有的类围绕 QTextDocument 展开, 它保证了用户可以创建和修改 富文本块, 而无须定义中间语言.一个 QTextDocument 可以通过两个接口操作, 一个是用于编辑的 C ...

  3. mysql 5.5 mysqldump备份原理

    开启general_log日志,获取mysqldump执行语句 show VARIABLES like 'general_log%' set GLOBAL general_log=on 执行备份命令 ...

  4. Listview右侧 IndexBar

    qq 好友聊天界面,右侧 IndexBar  A B C D ,点击跳转到相应的联系人名字 import android.content.Context; import android.graphic ...

  5. PHP判断一个变量是否可以通过foreach进行遍历

    <?php if( !is_array( $items ) && !$items instanceof Traversable ) //Throw exception here ...

  6. 利用css来让一个div在页面中垂直居中的方法

    一.如何让一个div在页面中垂直居中(请至少列出三种) 1.距离页面窗口左边框和上边框的距离设置为50%,这个50%就是指页面窗口的宽度和高度的50%,最后将该DIV分别左移和上移,左移和上移的大小就 ...

  7. OSI七层模型详解

    OSI 七层模型通过七个层次化的结构模型使不同的系统不同的网络之间实现可靠的通讯,因此其最主要的功能就是帮助不同类型的主机实现数据传输 . 完成中继功能的节点通常称为中继系统.在OSI七层模型中,处于 ...

  8. 一些Android经验

    1.如果在调试Android程序中,你非常确定你的代码是没有问题的,比如在跟Server交互时候,抓包软件抓到的包是正常的,但是在解析数据时候有问题, 你可以试着换个Android设备看看,模拟器换成 ...

  9. Zeppelin0.5.6使用hive解释器

    此zeppelin为官方0.5.6版,可能还在孵化阶段,可能出现一些bug吧. 配置 cp zeppelin-env.sh.template zeppelin-env.sh vi zeppelin-e ...

  10. 精通javasCRIPT-学习笔记 Features,Functions,Object

    关于学习javasCRIPT的书,市面上太多了,有犀牛书,红宝书,但是他们都类似于一本书,包含js的一切,并没有站在一个高度上,看js的本质,我计划看一看jquery作者resig写的<精通ja ...