ETH官方钱包

前往
大廳
主題

[LeetCode]1441. Build an Array With Stack Operations

テリ君(桃夫模式) | 2023-11-03 19:47:44 | 巴幣 2 | 人氣 133

說實(shí)在今天好像沒有難到可以被標(biāo)為Medium
這個跟火車進(jìn)站的概念挺像的
class Solution:
    def buildArray(self, target, n: int):
        res = []
        index = 0 # target's index
        curr = 1 # curr num
        while index < len(target):
            if target[index] == curr:
                res.append("Push")
                index += 1
            else:
                res.append("Push")
                res.append("Pop")
            curr += 1

        return res

sol = Solution()

print(sol.buildArray([1, 3], 3))
print(sol.buildArray([1, 2, 3], 3))
print(sol.buildArray([1, 2], 4))

創(chuàng)作回應(yīng)

相關(guān)創(chuàng)作

更多創(chuàng)作