Skip to content

Commit da2bd34

Browse files
committed
feat: update solutions to leetcode problem: No.0456
1 parent 95c5912 commit da2bd34

File tree

3 files changed

+67
-4
lines changed

3 files changed

+67
-4
lines changed

solution/0400-0499/0456.132 Pattern/README.md

+29-2
Original file line numberDiff line numberDiff line change
@@ -43,22 +43,49 @@
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
### **...**

solution/0400-0499/0456.132 Pattern/README_EN.md

+27-2
Original file line numberDiff line numberDiff 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 numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
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

0 commit comments

Comments
 (0)