[LeetCode] 232. Implement Queue using Stacks
Implement the following operations of a queue using stacks.
- push(x) -- Push element x to the back of queue.
- pop() -- Removes the element from in front of queue.
- peek() -- Get the front element.
- empty() -- Return whether the queue is empty.
這題 在 有閒來坐剪紙藝術公司愛考, 在pop() 的地方有個小 tricks
必須搞的水晶般透徹(crystal clear), 解法就是如果 stack two 不是空的就直接 s2.pop()
如果s2 是空的 就 把s1的內容丟過來
留言
張貼留言