File tree Expand file tree Collapse file tree 3 files changed +67
-4
lines changed
solution/0400-0499/0456.132 Pattern Expand file tree Collapse file tree 3 files changed +67
-4
lines changed Original file line number Diff line number Diff line change 4343
4444<!-- 这里可写通用的实现逻辑 -->
4545
46+ 单调栈实现。
47+
4648<!-- tabs:start -->
4749
4850### ** Python3**
4951
5052<!-- 这里可写当前语言的特殊实现逻辑 -->
5153
5254``` python
53-
55+ class Solution :
56+ def find132pattern (self , nums : List[int ]) -> bool :
57+ ak = float (' -inf' )
58+ stack = []
59+ for num in nums[::- 1 ]:
60+ if num < ak:
61+ return True
62+ while stack and num > stack[- 1 ]:
63+ ak = stack.pop()
64+ stack.append(num)
65+ return False
5466```
5567
5668### ** Java**
5769
5870<!-- 这里可写当前语言的特殊实现逻辑 -->
5971
6072``` java
61-
73+ class Solution {
74+ public boolean find132pattern (int [] nums ) {
75+ int ak = Integer . MIN_VALUE ;
76+ Deque<Integer > stack = new ArrayDeque<> ();
77+ for (int i = nums. length - 1 ; i >= 0 ; -- i) {
78+ if (nums[i] < ak) {
79+ return true ;
80+ }
81+ while (! stack. isEmpty() && nums[i] > stack. peek()) {
82+ ak = stack. pop();
83+ }
84+ stack. push(nums[i]);
85+ }
86+ return false ;
87+ }
88+ }
6289```
6390
6491### ** ...**
Original file line number Diff line number Diff line change @@ -73,13 +73,38 @@ that <b>i</b> < <b>j</b> < <b>k</b> and a<sub><b>i</b></sub> < a<sub><b>k</b></s
7373### ** Python3**
7474
7575``` python
76-
76+ class Solution :
77+ def find132pattern (self , nums : List[int ]) -> bool :
78+ ak = float (' -inf' )
79+ stack = []
80+ for num in nums[::- 1 ]:
81+ if num < ak:
82+ return True
83+ while stack and num > stack[- 1 ]:
84+ ak = stack.pop()
85+ stack.append(num)
86+ return False
7787```
7888
7989### ** Java**
8090
8191``` java
82-
92+ class Solution {
93+ public boolean find132pattern (int [] nums ) {
94+ int ak = Integer . MIN_VALUE ;
95+ Deque<Integer > stack = new ArrayDeque<> ();
96+ for (int i = nums. length - 1 ; i >= 0 ; -- i) {
97+ if (nums[i] < ak) {
98+ return true ;
99+ }
100+ while (! stack. isEmpty() && nums[i] > stack. peek()) {
101+ ak = stack. pop();
102+ }
103+ stack. push(nums[i]);
104+ }
105+ return false ;
106+ }
107+ }
83108```
84109
85110### ** ...**
Original file line number Diff line number Diff line change 1+ class Solution :
2+ def find132pattern (self , nums : List [int ]) -> bool :
3+ ak = float ('-inf' )
4+ stack = []
5+ for num in nums [::- 1 ]:
6+ if num < ak :
7+ return True
8+ while stack and num > stack [- 1 ]:
9+ ak = stack .pop ()
10+ stack .append (num )
11+ return False
You can’t perform that action at this time.
0 commit comments