资讯专栏INFORMATION COLUMN

[Leetcode] Implement Stack using Queues 用队列实现栈

ivan_qhz / 2516人阅读

摘要:双队列法复杂度时间空间思路和类似,我们也可以用两个队列来模拟栈的操作。当时,我们将数字进非空的队列就行了。操作和一样,区别在于我们拿到最后一个数后,还要再把它进另一个队列中。

双队列法 复杂度

时间 O(N) 空间 O(N)

思路

和Implement Queue using Stack类似,我们也可以用两个队列来模拟栈的操作。当push时,我们将数字offer进非空的队列就行了。当pop时,因为要拿的是队列最后一个数,我们先将它前面的数offer进另一个队列,然后多带带拿出最后一个数,并且不再offer进另一个队列,这样,另一个队列就少了最后一个数,而我们也拿到了最后一个数,而本来的队列就变成空了,等待下次的pop操作来填满。top操作和pop一样,区别在于我们拿到最后一个数后,还要再把它offer进另一个队列中。

代码

</>复制代码

  1. class MyStack {
  2. Queue queue1;
  3. Queue queue2;
  4. int size;
  5. public MyStack(){
  6. this.queue1 = new LinkedList();
  7. this.queue2 = new LinkedList();
  8. this.size = 0;
  9. }
  10. // Push element x onto stack.
  11. public void push(int x) {
  12. if(queue1.isEmpty()){
  13. queue2.offer(x);
  14. } else {
  15. queue1.offer(x);
  16. }
  17. size++;
  18. }
  19. // Removes the element on top of the stack.
  20. public int pop() {
  21. if(size == 0){
  22. return -1;
  23. }
  24. int res = 0;
  25. // 将前面的数都offer进另一个队列,然后拿出最后一个数
  26. if(queue1.isEmpty()){
  27. for(int i = 0; i < size - 1; i++){
  28. queue1.offer(queue2.poll());
  29. }
  30. res = queue2.poll();
  31. } else {
  32. for(int i = 0; i < size - 1; i++){
  33. queue2.offer(queue1.poll());
  34. }
  35. res = queue1.poll();
  36. }
  37. size--;
  38. return res;
  39. }
  40. // Get the top element.
  41. public int top() {
  42. if(size == 0){
  43. return -1;
  44. }
  45. // 先执行pop
  46. int top = pop();
  47. // 然后将pop出来的数再塞回队列就行了
  48. if(queue1.isEmpty()){
  49. queue2.offer(top);
  50. } else {
  51. queue1.offer(top);
  52. }
  53. // 注意size也要加1
  54. size++;
  55. return top;
  56. }
  57. // Return whether the stack is empty.
  58. public boolean empty() {
  59. return size == 0;
  60. }
  61. }

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/64618.html

相关文章

  • LeetCode 225:队列实现 Implement Stack using Queues

    摘要:下面是入栈时代码获得队列长度反转次数为队列长度减一反转语言没有栈和队列数据结构,只能用数组或双端队列实现。这类编程语言就压根不需要用队列实现栈或用栈实现队列这种问题,因为栈和队列本身就必须借助实现。 题目: 使用队列实现栈的下列操作: push(x) -- 元素 x 入栈 pop() -- 移除栈顶元素 top() -- 获取栈顶元素 empty() -- 返回栈是否为空 Impl...

    AlanKeene 评论0 收藏0
  • leetcode225 implement stack using queues

    摘要:题目要求使用队列来模拟实现一个栈。栈是指先进后出的数据结构,而队列则是先进先出的数据结构。队列的包括在队列尾插入数据,输出队列头的数据,查看队列的长度,队列是否为空。 题目要求 Implement the following operations of a queue using stacks. push(x) -- Push element x to the back of que...

    binta 评论0 收藏0
  • LeetCode 232:实现队列 Implement Queue using Stacks

    摘要:题目使用栈实现队列的下列操作将一个元素放入队列的尾部。用栈实现队列,可以用两个栈完成题解。入队列时用存入节点,出队列时内节点顺序出栈压入中。这类编程语言就压根不需要用队列实现栈或用栈实现队列这种问题,因为栈和队列本身就必须借助实现。 题目: 使用栈实现队列的下列操作: push(x) -- 将一个元素放入队列的尾部。 pop() -- 从队列首部移除元素。 peek() -- 返回队...

    cloud 评论0 收藏0
  • [Leetcode] Implement Queue using Stacks 实现队列

    摘要:注意的方法是和,实际上我们应该实现的是和或者和,的实现和是一样的,但将改为时,我们要先把到的元素保存,然后再弹出输出栈,然后返回这个保存的元素。 Implement Queue using Stacks Implement the following operations of a queue using stacks. push(x) -- Push element x to th...

    Martin91 评论0 收藏0
  • leetcode232 Implement Queue using Stacks

    摘要:题目要求通过队列实现一个栈的功能。栈的为压入栈顶,出栈,栈顶元素,栈是否为空。重复上述的操作。但是当需要弹出元素时,则从桶弹出。这样,下次加入的元素必然全部位于桶后的所有元素,而桶中的元素也能保证位输入顺序。极大的减少了不必要的入栈出栈。 题目要求 Implement the following operations of a queue using stacks. push(x) ...

    golden_hamster 评论0 收藏0

发表评论

0条评论

ivan_qhz

|高级讲师

TA的文章

阅读更多
最新活动
阅读需要支付1元查看
<