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. es5 数组

    一. 数组申明 var a = new Array(5)  会生成一个长度为5 每个元素都是undifined的数组 var a = new Array(1,2,3) 跟 var a = [1,2,3 ...

  2. Redis安装(CentOS7/tar.gz)

    1. 将安装包redis-3.2.0.tar.gz上传到linux系统,位置随意. 2. 解压文件 .tar.gz 3. 解压后会在当前目录生成文件夹“redis-3.2.0”,将其拷贝到" ...

  3. C# 中 重载,重写,隐藏的区别

    重载: 就是写多个同名方法,参数个数不同或类型不同或返回值不同 重写:子类中实现的方法必须加override关键词   普通非抽象父类需要virtual 抽象类里面抽象方法abstract 接口的实现 ...

  4. mysql中You can't specify target table for update in FROM clause

    使用mysql在删除表中重复记录 delete from user where username in (select user name form(select username from user ...

  5. 记JavaScript的入门学习(二)

    2016年11月25号,利用上午时间学习了JavaScript的数据类型和变量,下午就该去图书馆泡书了. 看完变量的本章节,发现我可能不能一天结束,那我就利用上午和晚上九点回来的时间完成吧.把心态调整 ...

  6. MyEclipse 代码提示设置

    打开 Eclipse -> Window -> Perferences -> Java -> Editor -> Content Assist,在右边最下面一栏找到 au ...

  7. Git 常用命令 更新与提交

    整理了一下Git 常用命令,这个版本还是比较好用的,最后附上个人终结版,帮助你快速上手. 取得Git仓库 初始化一个版本仓库 git init Clone远程版本库 git clone yourgit ...

  8. assert的基本用法

    assertion(断言)在软件开发中是一种常用的调试方式,很多开发语言中都支持这种机制,如C,C++和Eiffel等,但是支持的形式不尽相同,有的是通过语言本身.有的是通过库函数等.另外,从理论上来 ...

  9. hdu1035

    #include<stdio.h>#include<string.h>int step,n,m;int a[1010][1010];char map[11][11];void ...

  10. hdu1030

    #include<stdio.h>#include<math.h>void find(int n,int &l,int &r,int &level){ ...