Skip to content

Commit efdd2dc

Browse files
committed
一刷456
1 parent ed651cd commit efdd2dc

8 files changed

Lines changed: 101 additions & 37 deletions

File tree

README.adoc

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3218,12 +3218,12 @@ TIP: **公众号的微信号是: `jikerizhi`**。__因为众所周知的原因
32183218
|Easy
32193219
|
32203220

3221-
//|{counter:codes}
3222-
//|{leetcode_base_url}/132-pattern/[456. 132 Pattern^]
3223-
//|{source_base_url}/_0456_132Pattern.java[Java]
3224-
//|{doc_base_url}/0456-132-pattern.adoc[题解]
3225-
//|Medium
3226-
//|
3221+
|{counter:codes}
3222+
|{leetcode_base_url}/132-pattern/[456. 132 Pattern^]
3223+
|{source_base_url}/_0456_132Pattern.java[Java]
3224+
|{doc_base_url}/0456-132-pattern.adoc[题解]
3225+
|Medium
3226+
|
32273227

32283228
|{counter:codes}
32293229
|{leetcode_base_url}/circular-array-loop/[457. Circular Array Loop^]

docs/0456-132-pattern.adoc

Lines changed: 54 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,57 +1,81 @@
11
[#0456-132-pattern]
2-
= 456. 132 Pattern
2+
= 456. 132 模式
33

4-
{leetcode}/problems/132-pattern/[LeetCode - 132 Pattern^]
4+
https://leetcode.cn/problems/132-pattern/[LeetCode - 456. 132 模式 ^]
55

6+
给你一个整数数组 `nums` ,数组中共有 `n` 个整数。*132 模式的子序列* 由三个整数 `nums[i]``nums[j]``nums[k]` 组成,并同时满足:`+i < j < k+``+nums[i] < nums[k] < nums[j]+`
67

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`
99

10-
*Note:* n will be less than 15,000.
10+
*示例 1:*
1111

12-
*Example 1:*
12+
....
13+
输入:nums = [1,2,3,4]
14+
输出:false
15+
解释:序列中不存在 132 模式的子序列。
16+
....
1317

18+
*示例 2:*
1419

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+
....
1825

19-
*Output:* False
26+
*示例 3:*
2027

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+
....
2333

34+
*提示:*
2435

25-
*Example 2:*
36+
* `+n == nums.length+`
37+
* `1 \<= n \<= 2 * 10^5^`
38+
* `-10^9^ \<= nums[i] \<= 10^9^`
2639
2740
28-
[subs="verbatim,quotes,macros"]
29-
----
30-
*Input:* [3, 1, 4, 2]
3141
32-
*Output:* True
42+
== 思路分析
3343

34-
*Explanation:* There is a 132 pattern in the sequence: [1, 4, 2].
35-
----
44+
完全没有想到竟然可以使用单调栈来解决!
3645

46+
image::images/0456-10.jpeg[{image_attr}]
3747

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}]
4949

50+
image::images/0456-12.jpeg[{image_attr}]
5051

52+
从右向左遍历,使用一个单调递减栈,来维护 `32` 的关系:弹出的就是 `2`,栈中的就是 `3`,当前元素小于 `k` 时,就找到了 `132` 模式。
5153

5254
[[src-0456]]
55+
[tabs]
56+
====
57+
一刷::
58+
+
59+
--
5360
[{java_src_attr}]
5461
----
5562
include::{sourcedir}/_0456_132Pattern.java[tag=answer]
5663
----
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+
== 参考资料
5778

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 模式 - 官方题解^]

docs/images/0456-10.jpeg

228 KB
Loading

docs/images/0456-11.jpeg

152 KB
Loading

docs/images/0456-12.jpeg

396 KB
Loading

docs/index.adoc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -997,7 +997,7 @@ include::0454-4sum-ii.adoc[leveloffset=+1]
997997

998998
include::0455-assign-cookies.adoc[leveloffset=+1]
999999

1000-
// include::0456-132-pattern.adoc[leveloffset=+1]
1000+
include::0456-132-pattern.adoc[leveloffset=+1]
10011001

10021002
include::0457-circular-array-loop.adoc[leveloffset=+1]
10031003

logbook/202503.adoc

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1389,6 +1389,10 @@ endif::[]
13891389
|{doc_base_url}/0457-circular-array-loop.adoc[题解]
13901390
|✅ 使用回溯,对每一个元素做深度优先遍历。遇到环形数组则返回,否则继续向前遍历,直到结束。优化:遍历过的节点肯定不是环形数组,遇到遍历过的节点,直接“返回 false”,这样,所有节点只需要出来一次,时间复杂度 stem:[O(n)]。
13911391

1392+
|{counter:codes}
1393+
|{leetcode_base_url}/132-pattern/[456. 132 模式^]
1394+
|{doc_base_url}/0456-132-pattern.adoc[题解]
1395+
|❌ 单调栈!完全没想到单调栈的解法!从右向左遍历,使用一个单调递减栈,来维护 `32` 的关系:弹出的就是 `2`,栈中的就是 `3`,当前元素小于 `k` 时,就找到了 `132` 模式。
13921396

13931397
|===
13941398

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package com.diguage.algo.leetcode;
2+
3+
import java.util.ArrayDeque;
4+
import java.util.Deque;
5+
6+
public class _0456_132Pattern {
7+
// tag::answer[]
8+
/**
9+
* @author D瓜哥 · https://www.diguage.com
10+
* @since 2025-08-24 19:46:45
11+
*/
12+
public boolean find132pattern(int[] nums) {
13+
int n = nums.length;
14+
Deque<Integer> stack = new ArrayDeque<>();
15+
int k = Integer.MIN_VALUE;
16+
for (int i = n - 1; i >= 0; i--) {
17+
// k 初始化时是 Integer.MIN_VALUE,
18+
// 当出现小于 k 的元素时,说明 k 被更新过了,
19+
// 也说明栈中有元素且比 k 大
20+
if (nums[i] < k) {
21+
return true;
22+
}
23+
// 单调递减栈,遇到大的元素,就把小的都弹出来
24+
while (!stack.isEmpty() && stack.peekLast() < nums[i]) {
25+
// 在弹出来的小的元素里面,找最大的元素
26+
k = Math.max(k, stack.pollLast());
27+
}
28+
stack.addLast(nums[i]);
29+
}
30+
return false;
31+
}
32+
// end::answer[]
33+
public static void main(String[] args) {
34+
new _0456_132Pattern().find132pattern(new int[]{1, -4, 2, -1, 3, -3, -4, 0, -3, -1});
35+
}
36+
}

0 commit comments

Comments
 (0)