Matt Zheng

132-pattern

題目概述

題目

給定輸入矩陣,檢查是否有 “小、大、中” 的子集。

解法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
class Solution:
def find132pattern(self, nums: List[int]) -> bool:
length = len(nums)
data = []
ans = []
for i in range(length):
if len(data) > 1:
out = False
for j in range(0, len(ans), 2):
if data[0] == ans[j] and data[-1] == ans[j+1]: out = True
if data[0] <= ans[j] and data[-1] > ans[j]: out = True
if data[0] < ans[j+1] and data[-1] >= ans[j+1]: out = True
if out:
ans[j] = data[0]
ans[j+1] = data[-1]
else:
ans.append(data[0])
ans.append(data[-1])
#if nums[i] < data[0]: return True
while len(data) != 0 and data[-1] > nums[i]: data.pop(-1)
if len(ans) > 1:
#print(ans)
for j in range(0, len(ans), 2):
#print(j)
#print(nums[i])
if nums[i] > ans[j] and nums[i] < ans[j+1]: return True
data.append(nums[i])
#print(data)
return False