leetcode-栈- [剑指 Offer 09] 用两个栈实现队列 [面试题 03.04] 化栈为队

解题思路

两题都是相同题型通过栈实现队列 栈是先进后出的 所以只能使用数组的 pushpop 方法 myQueue.peek() 返回栈顶元素,但不在堆栈中删除它。 myQueue.pop() 返回栈顶元素,并在进程中删除它。 myQueue.empty() 如果队列是空的,则返回true,当堆栈包含元素时,返回false

  1. // 如 现在有两个栈
  2. stack1 = []
  3. stack2 = []
  4. // 我们用 push 往stack1里加入几个元素
  5. stack1 = [1, 2, 3]
  6. // 现在我们要一个一个地取出头部的元素
  7. // 我们可以循环 stack1, 把 stack1 元素一个个取出来 放入到 stack2中
  8. stack1 = []
  9. stack2 = [3, 2, 1]
  10. // 这样就可以用 pop 取出 头部元素了, 其实就是翻转数组
  11. // 如果stack2不为空 我们就一直从 stack2 中取, 直到为空了 又重新把 stack1 中的元素放入 stack2 中
  12. //如
  13. stack1 = [4, 5]
  14. stack2 = [3, 2]
  15. // 现在 2 中有元素, 要出队的话直接从 2 中出, 直到为空
  16. stack2.pop()
  17. stack2.pop() // 现在为空了
  18. // 重新循环 1 , 回到了上面开始的情况
  19. stack1 = []
  20. stack2 = [5, 4]

一 用两个栈实现队列

用两个栈实现一个队列。队列的声明如下,请实现它的两个函数 appendTail 和 deleteHead ,分别完成在队列尾部插入整数和在队列头部删除整数的功能。(若队列中没有元素,deleteHead 操作返回 -1 )

示例 :

  1. 输入:
  2. ["CQueue","appendTail","deleteHead","deleteHead"]
  3. [[],[3],[],[]]
  4. 输出:[null,null,3,-1]
  5. 示例 2
  6. 输入:
  7. ["CQueue","deleteHead","appendTail","appendTail","deleteHead","deleteHead"]
  8. [[],[],[5],[2],[],[]]
  9. 输出:[null,-1,null,null,5,2]
  10. 提示:
  11. 1 <= values <= 10000
  12. 最多会对 appendTaildeleteHead 进行 10000 次调用

题解

  1. var CQueue = function () {
  2. this.stack1 = [];
  3. this.stack2 = [];
  4. };
  5. /**
  6. * @param {number} value
  7. * @return {void}
  8. */
  9. CQueue.prototype.appendTail = function (value) {
  10. this.stack1.push(value);
  11. };
  12. /**
  13. * @return {number}
  14. */
  15. CQueue.prototype.deleteHead = function () {
  16. //为空将stack1的数组赋值给stack2;
  17. if (this.stack2.length === 0) {
  18. while (this.stack1.length > 0) {
  19. this.stack2.push(this.stack1.pop());
  20. }
  21. }
  22. return this.stack2.pop() || -1;
  23. };
  24. /**
  25. * Your CQueue object will be instantiated and called as such:
  26. * var obj = new CQueue()
  27. * obj.appendTail(value)
  28. * var param_2 = obj.deleteHead()
  29. */

二 化栈为队

实现一个MyQueue类,该类用两个栈来实现一个队列。

示例:

  1. MyQueue queue = new MyQueue();
  2. queue.push(1);
  3. queue.push(2);
  4. queue.peek(); // 返回 1
  5. queue.pop(); // 返回 1
  6. queue.empty(); // 返回 false

说明:

  • 你只能使用标准的栈操作 — 也就是只有 push to top, peek/pop from top, size 和 is empty 操作是合法的。
  • 你所使用的语言也许不支持栈。你可以使用 list 或者 deque(双端队列)来模拟一个栈,只要是标准的栈操作即可。
  • 假设所有操作都是有效的 (例如,一个空的队列不会调用 pop 或者 peek 操作)。

题解:

  1. /**
  2. * Initialize your data structure here.
  3. */
  4. var MyQueue = function () {
  5. this.stack1 = [];
  6. this.stack2 = [];
  7. };
  8. /**
  9. * Push element x to the back of queue.
  10. * @param {number} x
  11. * @return {void}
  12. */
  13. MyQueue.prototype.push = function (x) {
  14. this.stack1.push(x);
  15. };
  16. /**
  17. * Removes the element from in front of queue and returns that element.
  18. * @return {number}
  19. */
  20. MyQueue.prototype.pop = function () {
  21. if (!this.stack2.length) {
  22. while (this.stack1.length) {
  23. this.stack2.push(this.stack1.pop());
  24. }
  25. }
  26. return this.stack2.pop();
  27. };
  28. /**
  29. * Get the front element.
  30. * @return {number}
  31. */
  32. MyQueue.prototype.peek = function () {
  33. if (!this.stack2.length) {
  34. while (this.stack1.length) {
  35. this.stack2.push(this.stack1.pop());
  36. }
  37. }
  38. return this.stack2[this.stack2.length - 1];
  39. };
  40. /**
  41. * Returns whether the queue is empty.
  42. * @return {boolean}
  43. */
  44. MyQueue.prototype.empty = function () {
  45. return !this.stack2.length;
  46. };
  47. /**
  48. * Your MyQueue object will be instantiated and called as such:
  49. * var obj = new MyQueue()
  50. * obj.push(x)
  51. * var param_2 = obj.pop()
  52. * var param_3 = obj.peek()
  53. * var param_4 = obj.empty()
  54. */
  55. let queue = new MyQueue();
  56. queue.push(1);
  57. queue.push(2);
  58. console.log(queue.peek());// 返回 1
  59. console.log(queue.pop());// 返回 1
  60. console.log(queue.empty()); // 返回 false