很久没更LeetCode了,自己记录一下吧
题目:
代码一【栈的方法】复杂度n:
思路:(维护一个stack,以及第三个数字third --> for loop过程中不断更新更合适的 second > third,以及搜索first<third )
class Solution:
def find132pattern(self, nums: List[int]) -> bool:
third = float('-inf')
stack = []
for i in range(len(nums)-1, -1, -1):
if nums[i] < third:
return True
else:
while stack and stack[-1] < nums[i]:
third = stack.pop()
stack.append(nums[i])
return False
代码一答案: