• Implementing Stacks Using Queues


    Implement the following operations of a stack using queues.

    • push(x) -- Push element x onto stack.
    • pop() -- Removes the element on top of the stack.
    • top() -- Get the top element.
    • empty() -- Return whether the stack is empty.

    Notes:

    • You must use only standard operations of a queue -- which means only push to backpeek/pop from frontsize, and is empty operations are valid.
    • Depending on your language, queue may not be supported natively. You may simulate a queue by using a list or deque (double-ended queue), as long as you use only standard operations of a queue.
    • You may assume that all operations are valid (for example, no pop or top operations will be called on an empty stack). 

    Update (2015-06-11):
    The class name of the Java function had been updated to MyStack instead of Stack.

    Analyse: Using two queues to store the information. q is used to mock the stack while help is used to store temporary values.

    1. Time Exceeded Limit Version

      It's because transfering temporary queue to the original queue costs a lot of time. 

     1 class Stack {
     2 private:
     3     queue<int> q;
     4     queue<int> help;
     5 public:
     6     // Push element x onto stack.
     7     void push(int x) {
     8         q.push(x);
     9     }
    10 
    11     // Removes the element on top of the stack.
    12     void pop() {
    13         while(q.size() > 1){
    14             help.push(q.front());
    15             q.pop();
    16         }
    17         q.pop();
    18         while(!help.empty()){
    19             q.push(help.front());
    20         }
    21     }
    22 
    23     // Get the top element.
    24     int top() {
    25         while(q.size() > 1){
    26             help.push(q.front());
    27             q.pop();
    28         }
    29         int result = q.front();
    30         while(!help.empty()){
    31             q.push(help.front());
    32         }
    33     }
    34 
    35     // Return whether the stack is empty.
    36     bool empty() {
    37         return q.empty();
    38     }
    39 };
    View Code

    2. Set a 2-size array to respectively represent the two queue. When it comes to copy the temporary queue to the original queue, just switch the index.

        Runtime: 0ms.

     1 class Stack {
     2 private:
     3     queue<int> q[2];
     4     int index = 0;
     5 public:
     6     // Push element x onto stack.
     7     void push(int x) {
     8         q[index].push(x);
     9     }
    10 
    11     // Removes the element on top of the stack.
    12     void pop() {
    13         while(q[index].size() > 1){ //transfer all other elements into the helper queue
    14             q[1 - index].push(q[index].front());
    15             q[index].pop();
    16         }
    17         q[index].pop();
    18         index = 1 - index;
    19     }
    20 
    21     // Get the top element.
    22     int top() {
    23         while(q[index].size() > 1){ //transfer all other elements into the helper queue
    24             q[1 - index].push(q[index].front());
    25             q[index].pop();
    26         }
    27         int result = q[index].front();
    28         q[1- index].push(result);
    29         q[index].pop();
    30         index = 1 - index;
    31         return result;
    32     }
    33 
    34     // Return whether the stack is empty.
    35     bool empty() {
    36         return q[index].empty();
    37     }
    38 };
  • 相关阅读:
    20180929 北京大学 人工智能实践:Tensorflow笔记02
    20180929 北京大学 人工智能实践:Tensorflow笔记01
    YOLOv3学习笔记
    编辑器上传漏洞
    IIS解析漏洞利用
    数据库备份及审查元素进行webshell上传
    burp suite 进行webshell上传
    BUGKU CFT初学之WEB
    CTFbugku--菜鸟初学
    理解PHP中的会话控制
  • 原文地址:https://www.cnblogs.com/amazingzoe/p/4738230.html
Copyright © 2020-2023  润新知