Leetcode 456. 132 Pattern

题目的意思就是给你一个数组 里面一堆数,你是否能找到这样三个数,第一个数比第三个数小,第二个数最大。比如 1, 3, 2 或者  4, 9, 5 这种,数字可以不连续在一起,但是要保证顺序不变,只要有这么一组数就可以。

这个题我做了一个小时没想到比较好的办法,去看了别人的思路,看懂以后回来自己实现的,不得不说,想法真的优秀。

思路是这样的,既然是三个数比较,我找到中间那个数就是 2 那个数,再找到一个比2大的, 再找到一个比2 小的,这样就存在这么一组数了。那如何能保证顺序没问题呢,从数组的后面遍历,设定一个临时变量temp 以及 一个栈,temp 用来保存 2 这个数,stack里面 用来保存 可以成为3 这个数,每次遍历到一个新的数字curr,先比较这个数有没有stack[-1]大,如果大的话,把栈里面所有比 当前数小的 出栈temp = stack.pop(),把当前数进栈。所以temp 就是一个比栈里小的数了,栈里存的就是一个较大的数,因为是从后面遍历且temp是从栈里出来的,所以顺序上也是没问题的。现在重复上面的过程,直到找到了一个 比temp 小的数,那么这组数就找到了。

两个地方注意一下啊

为什么是while 循环出栈 改成if 可以吗?

可以考虑这么一组数[2, 4, 2, 3]

第二个temp = stack.pop()这里 怎么理解,为什么最后一个pop的数可以成为 temp,那之前出去的数呢?

还可以考虑这么一组数[2, 4, 2, 3]

哈哈哈

原文地址:https://www.cnblogs.com/winder-knight/p/9767151.html

时间: 2024-07-30 13:01:47

Leetcode 456. 132 Pattern的相关文章

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——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模式

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 个

Leetcode: 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] 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.

LeetCode:Word Pattern - 字符串模式匹配

1.题目名称 Word Pattern(字符串模式匹配) 2.题目地址 https://leetcode.com/problems/word-pattern/ 3.题目内容 英文:Given a pattern and a string str, find if str follows the same pattern. 中文:给出一组模式(pattern)和一个字符串(str),查看字符串是否与模式匹配 例如: pattern = "abba",str = "dog cat

【Leetcode】Word Pattern

题目链接:https://leetcode.com/problems/word-pattern/ 题目: Given a pattern and a string str, find if str follows the same pattern. Here follow means a full match, such that there is a bijection between a letter in pattern and a non-empty word in str. Examp

LeetCode 290 Word Pattern(单词模式)(istringstream、vector、map)(*)

翻译 给定一个模式,和一个字符串str.返回str是否符合同样的模式. 这里的符合意味着全然的匹配,所以这是一个一对多的映射,在pattern中是一个字母.在str中是一个为空的单词. 比如: pattern = "abba". str = "dog cat cat dog" 应该返回真. pattern = "abba", str = "dog cat cat fish" 应该返回假. pattern = "aaa