ShannonHung / ShannonHung.github.io

2 stars 0 forks source link

LeetCode #155 Min Stack - 刷題之旅 | Shannon's Blog 🐟 技術 | 生活 | 旅行 #108

Open ShannonHung opened 3 months ago

ShannonHung commented 3 months ago

https://shannonhung.github.io/posts/leetcode-155-min-stack/

1 題目描述 這題的重點在於要實現一個可以取得最小值的 stack,並且要求時間複雜度為 O(1)。解題思路的重點是,你要怎麼確保在 push() 的時候,更新最小值,並且在 pop() 的時候,如果 pop() 出當前最小值時,也要更新最小值。 2 解法 2.1 我的解法 有沒有發現重點,當 pop() 和 push() 的時候,最小值也會有所更新,在 pop() 的時候,最小的值被剔除時,