Re: [闲聊] 每日LeetCode

楼主: Rushia (みけねこ的鼻屎)   2022-12-16 09:30:23
232. Implement Queue using Stacks
实作只用Stack来模拟Queue的行为。
Example:
Input
["MyQueue", "push", "push", "peek", "pop", "empty"]
[[], [1], [2], [], [], []]
Output
[null, null, null, 1, 1, false]
Explanation
MyQueue myQueue = new MyQueue();
myQueue.push(1); // queue is: [1]
myQueue.push(2); // queue is: [1, 2] (leftmost is front of the queue)
myQueue.peek(); // return 1
myQueue.pop(); // return 1, queue is [2]
myQueue.empty(); // return false
思路:
1.就...资料结构课本里面的东西,利用一个stack暂存顶层元素,拿到底层元素
之后再把剩余元素放回原stack即可。
Java Code:
作者: PyTorch (屁眼火炬)   2021-12-16 09:30:00
大师这什么鸡掰的题目= =有queue干嘛还要用stack去模拟= =
楼主: Rushia (みけねこ的鼻屎)   2022-12-16 09:31:00
还有要用queue模拟stackㄉ捏
作者: wwndbk (黑人问号)   2022-12-16 09:32:00
大师
作者: pandix (面包屌)   2022-12-16 09:43:00
复杂度感觉不太对 pop和peek这样变O(n)了
楼主: Rushia (みけねこ的鼻屎)   2022-12-16 09:49:00
O(1)那个是Follow-up不一样ㄅ
作者: pandix (面包屌)   2022-12-16 09:51:00
喔喔原来放在follow-up

Links booklink

Contact Us: admin [ a t ] ucptt.com