Day 20
Date: October 15, 2022 2:16 PM
LinkedIn: https://leetcode.cn/problems/build-an-array-with-stack-operations/description/
Title: 用栈操作构建数组
class Solution:
def buildArray(self, target: List[int], n: int) -> List[str]:
index = 0 # target下标
stack = [] # 栈
for i in range(1, n+1): # 遍历从1-n的数
if index < len(target):# 如果index没有超出target下标
stack.append("Push") # 不管等不等于目标值都要先PUSH
if target[index] == i: # 如果等于目标值,target的遍历的下标要加一
index += 1
else:
stack.append("Pop") # 不等于目标值则入栈POP
return stack