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. itoa()函数和atoi()函数

     1.int/float to string/array: C语言提供了几个标准库函数,可以将任意类型(整型.长整型.浮点型等)的数字转换为字符串,下面列举了各函数的方法及其说明.● itoa():将 ...

  2. JQUERY 特殊字符

    //替换特殊字符 $(this).val($(this).val().replace(/[~'!<>@#$%^&*()-+_=:]/g, ""));

  3. error: a label can only be part of a statement and a declaration is not a statement

    GCC: error: a label can only be part of a statement and a declaration is not a statement switch(a){ ...

  4. shrio初体验(1)

    p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Monaco; color: #e6427a } p.p2 { margin: 0.0px 0 ...

  5. CodeForces 710E Generate a String

    $SPFA$,优化,$dp$. 写了一个裸的$SPFA$,然后加了一点优化就过了,不过要$300$多$ms$. $dp$的话跑的就比较快了. $dp[i]$表示输入$i$个字符的最小花费. 首先$dp ...

  6. flex flashplayer 程序 和 air 程序 通过 LocalConnection 通信

    flashplayer 做控制端: <?xml version="1.0" encoding="utf-8"?> <s:Application ...

  7. Python学习笔记——基础篇【第七周】———进程、线程、协程篇(socket基础)

    http://www.cnblogs.com/wupeiqi/articles/5040823.htmlhttp://i.cnblogs.com/EditPosts.aspx?postid=55437 ...

  8. delphi倒计时按钮写法

    procedure TForm1.FormActivate(Sender: TObject); var i: Integer; begin btn8.Enabled:=False; do begin ...

  9. Openjudge-NOI题库-对齐输出

     题目描述 Description 读入三个整数,按每个整数占8个字符的宽度,右对齐输出它们.  输入输出格式 Input/output 输入格式: 只有一行,包含三个整数,整数之间以一个空格分开. ...

  10. 工具类 util.Date 日期类

    /** * @description format the time * @author xf.radish * @param {String} format The format your want ...