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. python虚拟环境的使用

    一. 安装 sudo apt-get install python-virtualenv 二. 创建环境 sudo virtualenv Myenv 创建完全隔离的Python环境,实质是创建了一个文 ...

  2. 图片,音频资源预加载和文档dom加载

    在项目中遇到一个问题,ajax请求音频资源,然后动态的插入到文档中,其中.原生的音频外观实在太丑了,而且每个浏览器的样式都不一样,采取了一个audio插件. 就遇到一个问题,请求后的直接调用插件的话, ...

  3. 安卓---android:versionCode和android:versionName 用途

    主要用于升级和自我识别,转自:http://blog.csdn.net/wh_19910525/article/details/8660416 Android的版本可以在androidmainfest ...

  4. js控制键盘按键(回车、空格)

    <script type="text/javascript">  $(function(){         $(document).keypress(function ...

  5. RubyMine 2016.1 下载 附注册激活码 破解版方法

    注册破解方法: 在要求输入注册的界面选择激活码,然后粘贴以下注册码: 43B4A73YYJ-eyJsaWNlbnNlSWQiOiI0M0I0QTczWVlKIiwibGljZW5zZWVOYW1lIj ...

  6. sqlcipher for android

    github 地址 https://github.com/sqlcipher/android-database-sqlcipher 官网教程 https://www.zetetic.net/sqlci ...

  7. 提示找不到xml配置文件

    ClassPathXmlApplicationContext("applicationContext.xml")默认文件夹是resouerces,所以要把xml文件放在这个下面.

  8. html基础及心得

    html开始 <adress></adress>斜体(地址) <em><em>斜体(表示强调) <code></code>插入一 ...

  9. MVC网站后台分离

    1.新建一个项目TestAdmin项目类型随便选一个,待会删掉,因为这里我只需要解决方案名称  [项目存放于F盘    F:\TestAdmin] 2.新建一个 ZGJ.Web 的前台MVC项目,将开 ...

  10. Tiny64140之初始化时钟

    简介: Tiny6410 时钟逻辑为整个芯片提供了3种时钟分别为FCLK.HCLK.PCLK有三个PLL 分别为APLL.MPLL.EPLL.   APLL 专用于CPU   MPLL 供AHB(存储 ...