【LeetCode刷题日志】232.用栈实现队列

﹏ヽ暗。殇╰゛Y 2024-04-22 04:38 143阅读 0赞

be4144439b7a441ea35f801e9265613e.png

  • ?个人主页:库库的里昂
  • ?C/C++领域新星创作者
  • ?欢迎 ?点赞✍评论⭐收藏
  • ✨收录专栏:LeetCode 刷题日志
  • ?希望作者的文章能对你有所帮助,有不足的地方请在评论区留言指正,大家一起学习交流!?

目录

1.题目描述

2.解题思路+代码实现

方法:双栈

思路及算法:

代码实现:


1.题目描述

OJ链接 【leetcode 题号:232.用栈实现队列】【难度:简单】

请你仅使用两个栈实现先入先出队列。队列应当支持一般队列支持的所有操作(pushpoppeekempty):

实现 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:

  1. 输入:
  2. ["MyQueue", "push", "push", "peek", "pop", "empty"]
  3. [[], [1], [2], [], [], []]
  4. 输出:
  5. [null, null, null, 1, 1, false]
  6. 解释:
  7. MyQueue myQueue = new MyQueue();
  8. myQueue.push(1); // queue is: [1]
  9. myQueue.push(2); // queue is: [1, 2] (leftmost is front of the queue)
  10. myQueue.peek(); // return 1
  11. myQueue.pop(); // return 1, queue is [2]
  12. myQueue.empty(); // return false

提示:

  • 1 <= x <= 9
  • 最多调用 100pushpoppeekempty
  • 假设所有操作都是有效的 (例如,一个空的队列不会调用 pop 或者 peek 操作)

进阶:

  • 你能否实现每个操作均摊时间复杂度为 O(1) 的队列?换句话说,执行 n 个操作的总时间复杂度为 O(n) ,即使其中一个操作可能花费较长时间。

2.解题思路+代码实现

方法:双栈
思路及算法:

将一个栈当作输入栈,用于压入push传入的数据;另一个栈当作输出栈,用于 pop和peek操作。

每次pop或peek时,若输出栈为空则将输入栈的全部数据依次弹出并压入输出栈,这样输出栈从栈顶往栈底的顺序就是队列从队首往队尾的顺序。

代码实现:
  1. typedef int STDataType;
  2. typedef struct Stack
  3. {
  4. STDataType* a;
  5. int top;
  6. int capacity;
  7. }ST;
  8. void STInit(ST* pst)
  9. {
  10. assert(pst);
  11. pst->a = NULL;
  12. pst->top = 0;
  13. pst->capacity = 0;
  14. }
  15. void STDestroy(ST* pst)
  16. {
  17. assert(pst);
  18. free(pst->a);
  19. pst->a = NULL;
  20. pst->top = 0;
  21. pst->capacity = 0;
  22. }
  23. void STPush(ST* pst, STDataType x)
  24. {
  25. if (pst->top == pst->capacity) {
  26. int newCapacity = pst->capacity == 0 ? 4 : pst->capacity * 2;
  27. STDataType* tmp = (STDataType*)realloc(pst->a, newCapacity * sizeof(STDataType));
  28. if (tmp == NULL) {
  29. perror("realloc fail");
  30. return;
  31. }
  32. pst->a = tmp;
  33. pst->capacity = newCapacity;
  34. }
  35. pst->a[pst->top] = x;
  36. pst->top++;
  37. }
  38. bool STEmpty(ST* pst)
  39. {
  40. assert(pst);
  41. return pst->top == 0;
  42. }
  43. void STPop(ST* pst)
  44. {
  45. assert(pst);
  46. assert(!STEmpty(pst));
  47. pst->top--;
  48. }
  49. STDataType STTop(ST* pst)
  50. {
  51. assert(pst);
  52. assert(!STEmpty(pst));
  53. return pst->a[pst->top - 1];
  54. }
  55. int STSize(ST* pst)
  56. {
  57. assert(pst);
  58. return pst->top;
  59. }
  60. //------以下为OJ提供-------
  61. typedef struct {
  62. ST pushst;
  63. ST popst;
  64. } MyQueue;
  65. MyQueue* myQueueCreate() {
  66. MyQueue* obj = (MyQueue*)malloc(sizeof(MyQueue));
  67. if (obj == NULL) {
  68. perror("malloc fail");
  69. return 0;
  70. }
  71. STInit(&obj->pushst);
  72. STInit(&obj->popst);
  73. return obj;
  74. }
  75. void myQueuePush(MyQueue* obj, int x) {
  76. STPush(&obj->pushst, x);
  77. }
  78. int myQueuePop(MyQueue* obj) {
  79. int front = myQueuePeek(obj);
  80. STPop(&obj->popst);
  81. return front;
  82. }
  83. int myQueuePeek(MyQueue* obj) {
  84. if (STEmpty(&obj->popst)) {
  85. while (!STEmpty(&obj->pushst)) {
  86. STPush(&obj->popst, STTop(&obj->pushst));
  87. STPop(&obj->pushst);
  88. }
  89. }
  90. return STTop(&obj->popst);
  91. }
  92. bool myQueueEmpty(MyQueue* obj) {
  93. return STEmpty(&obj->pushst) && STEmpty(&obj->popst);
  94. }
  95. void myQueueFree(MyQueue* obj) {
  96. STDestroy(&obj->pushst);
  97. STDestroy(&obj->popst);
  98. free(obj);
  99. }
  100. /**
  101. * Your MyQueue struct will be instantiated and called as such:
  102. * MyQueue* obj = myQueueCreate();
  103. * myQueuePush(obj, x);
  104. * int param_2 = myQueuePop(obj);
  105. * int param_3 = myQueuePeek(obj);
  106. * bool param_4 = myQueueEmpty(obj);
  107. * myQueueFree(obj);
  108. */

复杂度分析

  • 时间复杂度:push和empty为O(1),pop和peek为均摊O(1)。对于每个元素,至多入栈和出栈各两次,故均摊复杂度为O(1)。
  • 空间复杂度:O(n)。其中n是操作总数。对于有n次push操作的情况,队列中会有n个元素,故空间复杂度为O(n)。

a9b29b8482654533ae41edf11fc7ab6f.png

发表评论

表情:
评论列表 (有 0 条评论,143人围观)

还没有评论,来说两句吧...

相关阅读