Leetcode 456.132模式

132模式

给定一个整数序列:a1, a2, ..., an,一个132模式的子序列 ai, aj, ak 被定义为:当 i < j < k 时,ai < ak < aj。设计一个算法,当给定有 n 个数字的序列时,验证这个序列中是否含有132模式的子序列。

注意:n 的值小于15000。

示例1:

输入: [1, 2, 3, 4]

输出: False

解释: 序列中不存在132模式的子序列。

示例 2:

输入: [3, 1, 4, 2]

输出: True

解释: 序列中有 1 个132模式的子序列: [1, 4, 2].

示例 3:

输入: [-1, 3, 2, 0]

输出: True

解释: 序列中有 3 个132模式的的子序列: [-1, 3, 2], [-1, 3, 0] 和 [-1, 2, 0].

 1 import java.util.Stack;
 2
 3 class Solution {
 4     public boolean find132pattern(int[] nums) {
 5         if(nums.length<3) return false;
 6         Stack<Integer> s=new Stack<Integer>();
 7         int second=Integer.MIN_VALUE;
 8         for(int i=nums.length-1;i>=0;i--){
 9             if(nums[i]<second) return true;
10             while(!s.isEmpty()&&nums[i]>s.peek()){
11                 second=s.pop();
12             }
13             s.push(nums[i]);
14         }
15         return false;
16     }
17 }

原文地址:https://www.cnblogs.com/kexinxin/p/10269858.html

时间: 2024-07-30 12:56:22

Leetcode 456.132模式的相关文章

LeetCode——456.132模式

给定一个整数序列:a1, a2, ..., an,一个132模式的子序列 ai, aj, ak 被定义为:当 i < j < k 时,ai < ak < aj.设计一个算法,当给定有 n 个数字的序列时,验证这个序列中是否含有132模式的子序列. 注意:n 的值小于15000. 示例1: 输入: [1, 2, 3, 4] 输出: False 解释: 序列中不存在132模式的子序列. 示例 2: 输入: [3, 1, 4, 2] 输出: True 解释: 序列中有 1 个132模式的

Leetcode 456. 132 Pattern

题目的意思就是给你一个数组 里面一堆数,你是否能找到这样三个数,第一个数比第三个数小,第二个数最大.比如 1, 3, 2 或者  4, 9, 5 这种,数字可以不连续在一起,但是要保证顺序不变,只要有这么一组数就可以. 这个题我做了一个小时没想到比较好的办法,去看了别人的思路,看懂以后回来自己实现的,不得不说,想法真的优秀. 思路是这样的,既然是三个数比较,我找到中间那个数就是 2 那个数,再找到一个比2大的, 再找到一个比2 小的,这样就存在这么一组数了.那如何能保证顺序没问题呢,从数组的后面

[Swift]LeetCode456. 132模式 | 32 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.

[Leetcode456] 132模式 峰谷法/单调栈

题目:https://leetcode-cn.com/problems/132-pattern/ 思路: 如果某个数左边比它小的数的最小值,小于它右边小于它的某个数(不必找最大值),那么这个序列就符合132模式的定义.如下图三点所示. 于是有解法1(峰谷法): 1 public static boolean find132pattern(int[] nums) { 2 int min = Integer.MAX_VALUE; 3 int max = Integer.MIN_VALUE; 4 bo

[LeetCode] 132 Pattern 132模式

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.

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.

LC 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.

Leetcode 290. 单词模式

class Solution { public: bool wordPattern(string pattern, string str) { map<string, char> word_map; char used[128] = {0}; string word; int pos= 0; str.push_back(' '); for(int i=0; i<str.size(); ++i) { if(str[i] == ' ') { //结束条件 if(pos == pattern.

132模式

2018-09-27 23:20:20 问题描述: 问题求解: 本题的难度还是有的,主要的考虑方向是尽量构造[min, max]来将后面出现的数字插入到其中.这里的求解方法是使用Stack来维护一组non-overlapping的区间,每次判断当前的数字能够加入到区间之中,如果可以,那么就直接返回true,如果不可以,那么就需要维护这个区间栈.这里有个地方需要注意到的是在栈顶的元素的左边界是到当前的为止的最小值,换句话说,Stack中的区间是按照左边界进行排序的. public boolean