首页 > 编程语言 >代码随想录算法训练营第十天 | 理论基础,232.用栈实现队列,225. 用队列实现栈

代码随想录算法训练营第十天 | 理论基础,232.用栈实现队列,225. 用队列实现栈

时间:2023-01-28 11:33:39浏览次数:57  
标签:第十天 队列 top 随想录 pop int push empty

一、参考资料

理论基础

文章讲解:https://programmercarl.com/%E6%A0%88%E4%B8%8E%E9%98%9F%E5%88%97%E7%90%86%E8%AE%BA%E5%9F%BA%E7%A1%80.html

用栈实现队列

题目链接/文章讲解/视频讲解:https://programmercarl.com/0232.%E7%94%A8%E6%A0%88%E5%AE%9E%E7%8E%B0%E9%98%9F%E5%88%97.html

用队列实现栈

题目链接/文章讲解/视频讲解:https://programmercarl.com/0225.%E7%94%A8%E9%98%9F%E5%88%97%E5%AE%9E%E7%8E%B0%E6%A0%88.html

二、理论基础

  • 三个最为普遍的STL版本:

  1. HP STL 其他版本的C++ STL,一般是以HP STL为蓝本实现出来的,HP STL是C++ STL的第一个实现版本,而且开放源代码。

  1. P.J.Plauger STL 由P.J.Plauger参照HP STL实现出来的,被Visual C++编译器所采用,不是开源的。

  1. SGI STL 由Silicon Graphics Computer Systems公司参照HP STL实现,被Linux的C++编译器GCC所采用,SGI STL是开源软件,源码可读性甚高。

  • 栈的内部结构,栈的底层实现可以是vector,deque,list 都是可以的, 主要就是数组和链表的底层实现。

三、LeetCode232.用栈实现队列

https://leetcode.cn/problems/implement-queue-using-stacks/

请你仅使用两个栈实现先入先出队列。队列应当支持一般队列支持的所有操作(push、pop、peek、empty):
实现 MyQueue 类:
void push(int x) 将元素 x 推到队列的末尾
int pop() 从队列的开头移除并返回元素
int peek() 返回队列开头的元素
boolean empty() 如果队列为空,返回 true ;否则,返回 false
说明:
你 只能 使用标准的栈操作 —— 也就是只有 push to top, peek/pop from top, size, 和 is empty 操作是合法的。
你所使用的语言也许不支持栈。你可以使用 list 或者 deque(双端队列)来模拟一个栈,只要是标准的栈操作即可。

示例 1:
输入: ["MyQueue", "push", "push", "peek", "pop", "empty"] [[], [1], [2], [], [], []] 输出: [null, null, null, 1, 1, false] 解释: 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 <= x <= 9
最多调用 100 次 push、pop、peek 和 empty
假设所有操作都是有效的 (例如,一个空的队列不会调用 pop 或者 peek 操作)

进阶:
你能否实现每个操作均摊时间复杂度为 O(1) 的队列?换句话说,执行 n 个操作的总时间复杂度为 O(n) ,即使其中一个操作可能花费较长时间。
  1. class MyQueue {
  2. public:
  3. stack<int> stIn;
  4. stack<int> stOut;
  5. // 初始化数据结果
  6. MyQueue() {
  7. }
  8. // push(x) -- 将一个元素放入队列的尾部
  9. void push(int x) {
  10. stIn.push(x);
  11. }
  12. // pop() -- 从队列首部移除元素
  13. int pop() {
  14. // 只有当stOut为空的时候,再从stIn里导入数据(导入stIn全部数据)
  15. if (stOut.empty()) {
  16. // 从stIn导入数据直到stIn为空
  17. while (!stIn.empty()) {
  18. stOut.push(stIn.top());
  19. stIn.pop();
  20. }
  21. }
  22. int result = stOut.top();
  23. stOut.pop();
  24. return result;
  25. }
  26. // peek() -- 返回队列首部的元素
  27. int peek() {
  28. // 直接使用已有的pop函数
  29. int res = this->pop();
  30. // 因为pop函数弹出了元素res,所以再添加回去
  31. stOut.push(res);
  32. return res;
  33. }
  34. // empty() -- 返回队列是否为空
  35. bool empty() {
  36. return stIn.empty() && stOut.empty();
  37. }
  38. };
  39. /**
  40. * Your MyQueue object will be instantiated and called as such:
  41. * MyQueue* obj = new MyQueue();
  42. * obj->push(x);
  43. * int param_2 = obj->pop();
  44. * int param_3 = obj->peek();
  45. * bool param_4 = obj->empty();
  46. */

四、LeetCode225. 用队列实现栈

https://leetcode.cn/problems/implement-stack-using-queues/description/

请你仅使用两个队列实现一个后入先出(LIFO)的栈,并支持普通栈的全部四种操作(push、top、pop 和 empty)。
实现 MyStack 类:
void push(int x) 将元素 x 压入栈顶。
int pop() 移除并返回栈顶元素。
int top() 返回栈顶元素。
boolean empty() 如果栈是空的,返回 true ;否则,返回 false 。

注意:
你只能使用队列的基本操作 —— 也就是 push to back、peek/pop from front、size 和 is empty 这些操作。
你所使用的语言也许不支持队列。 你可以使用 list (列表)或者 deque(双端队列)来模拟一个队列 , 只要是标准的队列操作即可。

示例:
输入: ["MyStack", "push", "push", "top", "pop", "empty"] [[], [1], [2], [], [], []] 输出: [null, null, null, 2, 2, false] 解释: MyStack myStack = new MyStack(); myStack.push(1); myStack.push(2); myStack.top(); // 返回 2 myStack.pop(); // 返回 2 myStack.empty(); // 返回 False
提示:
1 <= x <= 9
最多调用100 次 push、pop、top 和 empty
每次调用 pop 和 top 都保证栈不为空

进阶:你能否仅用一个队列来实现栈。

两个队列版本代码实现-2023.1.20

  1. class MyStack {
  2. public:
  3. // 两个队列版本
  4. queue<int> que1;
  5. queue<int> que2; // 辅助队列,用来备份
  6. // 初始化数据结构
  7. MyStack() {
  8. }
  9. // push(x) -- 元素 x 入栈
  10. void push(int x) {
  11. que1.push(x);
  12. }
  13. // pop() -- 移除栈顶元素
  14. int pop() {
  15. int size = que1.size();
  16. size--;
  17. // 将que1导入que2,但要留下最后一个元素
  18. while (size--) {
  19. que2.push(que1.front());
  20. que1.pop();
  21. }
  22. // 留下的最后一个元素就是要返回的值
  23. int result = que1.front();
  24. que1.pop();
  25. // 再将que2赋值给que1
  26. que1 = que2;
  27. // 清空que2
  28. while (!que2.empty()) {
  29. que2.pop();
  30. }
  31. return result;
  32. }
  33. // top() -- 获取栈顶元素
  34. int top() {
  35. return que1.back();
  36. }
  37. // empty() -- 返回栈是否为空
  38. bool empty() {
  39. return que1.empty();
  40. }
  41. };
  42. /**
  43. * Your MyStack object will be instantiated and called as such:
  44. * MyStack* obj = new MyStack();
  45. * obj->push(x);
  46. * int param_2 = obj->pop();
  47. * int param_3 = obj->top();
  48. * bool param_4 = obj->empty();
  49. */

一个队列版本代码实现-2023.1.20 进阶

  1. class MyStack {
  2. public:
  3. // 一个队列实现版本
  4. queue<int> que;
  5. /** Initialize your data structure here. */
  6. MyStack() {
  7. }
  8. /** Push element x onto stack. */
  9. void push(int x) {
  10. que.push(x);
  11. }
  12. /** Removes the element on top of the stack and returns that element. */
  13. int pop() {
  14. int size = que.size();
  15. size--;
  16. while (size--) { // 将队列头部的元素(除了最后一个元素外) 重新添加到队列尾部
  17. que.push(que.front());
  18. que.pop();
  19. }
  20. int result = que.front(); // 此时弹出的元素顺序就是栈的顺序了
  21. que.pop();
  22. return result;
  23. }
  24. /** Get the top element. */
  25. int top() {
  26. return que.back();
  27. }
  28. /** Returns whether the stack is empty. */
  29. bool empty() {
  30. return que.empty();
  31. }
  32. };
  33. /**
  34. * Your MyStack object will be instantiated and called as such:
  35. * MyStack* obj = new MyStack();
  36. * obj->push(x);
  37. * int param_2 = obj->pop();
  38. * int param_3 = obj->top();
  39. * bool param_4 = obj->empty();
  40. */

总结:

  1. 过完年啦,抓紧时间收收心,该补的题及时补上,不能再偷懒!!!

  1. 栈和队列是基本数据结构中必须要掌握的,今天的俩题重点在于理解两种结构的底层实现原理,尽管在实际业务中不常有需求互相实现,但是对之后更难的专题训练理解有深刻意义。

  1. 落下一次又一次,只能是一次又一次跟不上,时间是挤出来的,懒散是不自律惯得。事实就在这儿放着,怎样行动看你啦

刷题加油鸭~~

标签:第十天,队列,top,随想录,pop,int,push,empty
From: https://www.cnblogs.com/ucaszym/p/17069962.html

相关文章