LeetCode 225. Implement Stack using Queues && 232. Implement Queue using Stacks

LeetCode 225. Implement Stack 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.

Example:

MyStack stack = new MyStack();

stack.push(1);
stack.push(2);  
stack.top();   // returns 2
stack.pop();   // returns 2
stack.empty(); // returns false

Notes:

  • You must use only standard operations of a queue -- which means only push to back, peek/pop from front, size, 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).

代碼:

class MyStack {
public:
    /** Initialize your data structure here. */
    queue <int> q; 
    MyStack() {
    }
    
    /** Push element x onto stack. */
    void push(int x) {
        q.push(x);
        for(int i=0; i<q.size()-1;i++){
            q.push(q.front());
            q.pop();
        }
    }
    
    /** Removes the element on top of the stack and returns that element. */
    int pop() {
        int out=q.front();
        q.pop();
        return out;
    }
    
    /** Get the top element. */
    int top() {
         return q.front();
    }
    
    /** Returns whether the stack is empty. */
    bool empty() {
       return q.empty();
    }
};

/**
 * Your MyStack object will be instantiated and called as such:
 * MyStack obj = new MyStack();
 * obj.push(x);
 * int param_2 = obj.pop();
 * int param_3 = obj.top();
 * bool param_4 = obj.empty();
 */

C++隊列使用方法:

#include <iostream>
#include <queue>
#include <assert.h>
/*
調用的時候要有頭文件: #include<stdlib.h> 或

#include<cstdlib> 

#include<queue>       #include<queue>
詳細用法:
定義一個queue的變量     queue<Type> M
查看是否爲空範例        M.empty()    是的話返回1,不是返回0;
從已有元素後面增加元素   M.push()
輸出現有元素的個數      M.size()
顯示第一個元素          M.front()
顯示最後一個元素        M.back()
清除第一個元素          M.pop()
*/

232. Implement Queue using Stacks

Implement the following operations of a queue using stacks.

  • push(x) -- Push element x to the back of queue.
  • pop() -- Removes the element from in front of queue.
  • peek() -- Get the front element.
  • empty() -- Return whether the queue is empty.

Example:

MyQueue queue = new MyQueue();

queue.push(1);
queue.push(2);  
queue.peek();  // returns 1
queue.pop();   // returns 1
queue.empty(); // returns false

Notes:

  • You must use only standard operations of a stack -- which means only push to top, peek/pop from top, size, and is empty operations are valid.
  • Depending on your language, stack may not be supported natively. You may simulate a stack by using a list or deque (double-ended queue), as long as you use only standard operations of a stack.
  • You may assume that all operations are valid (for example, no pop or peek operations will be called on an empty queue).
class MyQueue {
public:
    /** Initialize your data structure here. */
    stack<int> s1;
    stack<int> s2;
    MyQueue() {
        
    }
    
    /** Push element x to the back of queue. */
    void push(int x) {
        s1.push(x);      
    }
    
    /** Removes the element from in front of queue and returns that element. */
    int pop() {
        int out;
        if(s2.empty()){
            while(!s1.empty()){
                s2.push(s1.top());
                s1.pop();
            } 
            out = s2.top();
            s2.pop();      
        }else{
            out = s2.top();
            s2.pop();
        }
        return out;
    }
    
    /** Get the front element. */
    int peek() {
        if(s2.empty()){
            while(!s1.empty()){
                s2.push(s1.top());
                s1.pop();
            }   
            return s2.top();
        }else{
            return s2.top();
        }    
    }
    
    /** Returns whether the queue is empty. */
    bool empty() {
        return s1.empty()&&s2.empty();
    }
};

/**
 * Your MyQueue object will be instantiated and called as such:
 * MyQueue obj = new MyQueue();
 * obj.push(x);
 * int param_2 = obj.pop();
 * int param_3 = obj.peek();
 * bool param_4 = obj.empty();
 */

stack的用法:

  1. push(): 向棧內壓入一個成員;
  2. pop(): 從棧頂彈出一個成員;
  3. empty(): 如果棧爲空返回true,否則返回false;
  4. top(): 返回棧頂,但不刪除成員;
  5. size(): 返回棧內元素的大小;
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章