【LeetCode】456. 132 Pattern
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的更多相关文章
- 【LeetCode】456. 132 Pattern 解题报告(Python)
[LeetCode]456. 132 Pattern 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fu ...
- 【LeetCode】290. Word Pattern 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- 【leetcode】290. Word Pattern
problem 290. Word Pattern 多理解理解题意!!! 不过博主还是不理解,应该比较的是单词的首字母和pattern的顺序是否一致.疑惑!知道的可以分享一下下哈- 之前理解有误,应该 ...
- 456. 132 Pattern
456. 132 Pattern Given an array of integers a1, a2, a3-an, judge if there exists the 132 pattern. 13 ...
- 【leetcode】44. Wildcard Matching
题目如下: 解题思路:本题和[leetcode]97. Interleaving String非常相似,同样可以采用动态规划的方法.记dp[i][j] = 1或者0 表示pattern[0:i]是否匹 ...
- 【LeetCode】385. Mini Parser 解题报告(Python)
[LeetCode]385. Mini Parser 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode.com/problems/mini-parser/ ...
- 【LeetCode】Minimum Depth of Binary Tree 二叉树的最小深度 java
[LeetCode]Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum dept ...
- 【Leetcode】Pascal's Triangle II
Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3, Return [1,3 ...
- 53. Maximum Subarray【leetcode】
53. Maximum Subarray[leetcode] Find the contiguous subarray within an array (containing at least one ...
随机推荐
- oc内容5大区
1.堆区(malloc):不需要手动管理内存,自动管理 2.栈区(stack):需要手动管理内存 3.静态区 4.常量区 5.方法区 load类方法:把类加载进内存的时候调用,只会调用一次 initi ...
- VUGEN错误处理函数--lr-continue-on-error
void lr-continue-on-error(int value);value是脚本运行出错时的取值,具体取值与相应值得含义如下表,在具体使用时,可以取常量名或者常量值代表. 1.设置,选择co ...
- orm2
数据库连接 var orm = require("orm"); orm.connect("mysql://username:password@host/database& ...
- JS代码:设为首页 加入收藏
// JavaScript Document // 加入收藏 <a onclick="AddFavorite(window.location,document.title)" ...
- java自动装箱的陷阱
自动装箱和拆箱是java的一颗语法糖,在给我们带来使用便利的同时也带来一些疑惑,请看下面的代码: public class TestClass { public static void main(St ...
- Front-End(二)——HTML
本文主要对html迭代学习中的要点.冷点简述罗列. html之前也说过,主要为了描述页面的结构和内容,合理使用结构化的标签,<h1>.<div>等,有利于前端开发,也有利于搜索 ...
- idea空包自动叠加问题
取消上面选项就行了.
- 3.使用secureCRT连接PC,LINUX,开发板
1.设置secureCRT(可选项):http://www.linuxyw.com/linux/gongxiang/20130505/161.html 2.使用secureCRT远程登录linux 3 ...
- java 打开浏览器 url
public class openBrowers { public static void main(String[] args) { try { //String url = "http: ...
- ContourLine
#define MULTI_PLOT true //Determine whether or not to plot multiple iterations. #define X_MAX 1.0 // ...