File tree 3 files changed +67
-4
lines changed
solution/0400-0499/0456.132 Pattern
3 files changed +67
-4
lines changed Original file line number Diff line number Diff line change 43
43
44
44
<!-- 这里可写通用的实现逻辑 -->
45
45
46
+ 单调栈实现。
47
+
46
48
<!-- tabs:start -->
47
49
48
50
### ** Python3**
49
51
50
52
<!-- 这里可写当前语言的特殊实现逻辑 -->
51
53
52
54
``` 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
54
66
```
55
67
56
68
### ** Java**
57
69
58
70
<!-- 这里可写当前语言的特殊实现逻辑 -->
59
71
60
72
``` 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
+ }
62
89
```
63
90
64
91
### ** ...**
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
73
73
### ** Python3**
74
74
75
75
``` 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
77
87
```
78
88
79
89
### ** Java**
80
90
81
91
``` 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
+ }
83
108
```
84
109
85
110
### ** ...**
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