|
1 | 1 | [#0456-132-pattern] |
2 | | -= 456. 132 Pattern |
| 2 | += 456. 132 模式 |
3 | 3 |
|
4 | | -{leetcode}/problems/132-pattern/[LeetCode - 132 Pattern^] |
| 4 | +https://leetcode.cn/problems/132-pattern/[LeetCode - 456. 132 模式 ^] |
5 | 5 |
|
| 6 | +给你一个整数数组 `nums` ,数组中共有 `n` 个整数。*132 模式的子序列* 由三个整数 `nums[i]`、`nums[j]` 和 `nums[k]` 组成,并同时满足:`+i < j < k+` 和 `+nums[i] < nums[k] < nums[j]+` 。 |
6 | 7 |
|
7 | | -Given a sequence of n integers a~1~, a~2~, ..., a~n~, a 132 pattern is a subsequence a~*i*~, a~*j*~, a~*k*~ such |
8 | | -that *i* < *j* < *k* and a~*i*~ < a~*k*~ < a~*j*~. Design an algorithm that takes a list of n numbers as input and checks whether there is a 132 pattern in the list. |
| 8 | +如果 `nums` 中存在 *132 模式的子序列* ,返回 `true` ;否则,返回 `false` 。 |
9 | 9 |
|
10 | | -*Note:* n will be less than 15,000. |
| 10 | +*示例 1:* |
11 | 11 |
|
12 | | -*Example 1:* |
| 12 | +.... |
| 13 | +输入:nums = [1,2,3,4] |
| 14 | +输出:false |
| 15 | +解释:序列中不存在 132 模式的子序列。 |
| 16 | +.... |
13 | 17 |
|
| 18 | +*示例 2:* |
14 | 19 |
|
15 | | -[subs="verbatim,quotes,macros"] |
16 | | ----- |
17 | | -*Input:* [1, 2, 3, 4] |
| 20 | +.... |
| 21 | +输入:nums = [3,1,4,2] |
| 22 | +输出:true |
| 23 | +解释:序列中有 1 个 132 模式的子序列: [1, 4, 2] 。 |
| 24 | +.... |
18 | 25 |
|
19 | | -*Output:* False |
| 26 | +*示例 3:* |
20 | 27 |
|
21 | | -*Explanation:* There is no 132 pattern in the sequence. |
22 | | ----- |
| 28 | +.... |
| 29 | +输入:nums = [-1,3,2,0] |
| 30 | +输出:true |
| 31 | +解释:序列中有 3 个 132 模式的的子序列:[-1, 3, 2]、[-1, 3, 0] 和 [-1, 2, 0] 。 |
| 32 | +.... |
23 | 33 |
|
| 34 | +*提示:* |
24 | 35 |
|
25 | | -*Example 2:* |
| 36 | +* `+n == nums.length+` |
| 37 | +* `1 \<= n \<= 2 * 10^5^` |
| 38 | +* `-10^9^ \<= nums[i] \<= 10^9^` |
26 | 39 |
|
27 | 40 |
|
28 | | -[subs="verbatim,quotes,macros"] |
29 | | ----- |
30 | | -*Input:* [3, 1, 4, 2] |
31 | 41 |
|
32 | | -*Output:* True |
| 42 | +== 思路分析 |
33 | 43 |
|
34 | | -*Explanation:* There is a 132 pattern in the sequence: [1, 4, 2]. |
35 | | ----- |
| 44 | +完全没有想到竟然可以使用单调栈来解决! |
36 | 45 |
|
| 46 | +image::images/0456-10.jpeg[{image_attr}] |
37 | 47 |
|
38 | | -*Example 3:* |
39 | | - |
40 | | - |
41 | | -[subs="verbatim,quotes,macros"] |
42 | | ----- |
43 | | -*Input:* [-1, 3, 2, 0] |
44 | | -
|
45 | | -*Output:* True |
46 | | -
|
47 | | -*Explanation:* There are three 132 patterns in the sequence: [-1, 3, 2], [-1, 3, 0] and [-1, 2, 0]. |
48 | | ----- |
| 48 | +image::images/0456-11.jpeg[{image_attr}] |
49 | 49 |
|
| 50 | +image::images/0456-12.jpeg[{image_attr}] |
50 | 51 |
|
| 52 | +从右向左遍历,使用一个单调递减栈,来维护 `32` 的关系:弹出的就是 `2`,栈中的就是 `3`,当前元素小于 `k` 时,就找到了 `132` 模式。 |
51 | 53 |
|
52 | 54 | [[src-0456]] |
| 55 | +[tabs] |
| 56 | +==== |
| 57 | +一刷:: |
| 58 | ++ |
| 59 | +-- |
53 | 60 | [{java_src_attr}] |
54 | 61 | ---- |
55 | 62 | include::{sourcedir}/_0456_132Pattern.java[tag=answer] |
56 | 63 | ---- |
| 64 | +-- |
| 65 | +
|
| 66 | +// 二刷:: |
| 67 | +// + |
| 68 | +// -- |
| 69 | +// [{java_src_attr}] |
| 70 | +// ---- |
| 71 | +// include::{sourcedir}/_0456_132Pattern_2.java[tag=answer] |
| 72 | +// ---- |
| 73 | +// -- |
| 74 | +==== |
| 75 | + |
| 76 | + |
| 77 | +== 参考资料 |
57 | 78 |
|
| 79 | +. https://leetcode.cn/problems/132-pattern/solutions/676970/xiang-xin-ke-xue-xi-lie-xiang-jie-wei-he-95gt/[456. 132 模式 - 详解为何使用「单调栈」来找最大的 K 是正确的^] |
| 80 | +. https://leetcode.cn/problems/132-pattern/solutions/676741/fu-xue-ming-zhu-cong-bao-li-qiu-jie-dao-eg78f/[456. 132 模式 - 从「暴力求解」到「单调栈」^] |
| 81 | +. https://leetcode.cn/problems/132-pattern/solutions/676437/132mo-shi-by-leetcode-solution-ye89/[456. 132 模式 - 官方题解^] |
0 commit comments