Share my solution c++


  • 0
    P
    class Queue {
    public:
    	bool iss1;
    	stack<int> s1;
    	stack<int> s2;
    	
    	Queue() : iss1(true){}
    	
    	void push(int x)
    		{
    			if(!iss1)
    			{			
    				while(!s2.empty())
    				{
    					s1.push(s2.top());
    					s2.pop();
    				}	
    				iss1 = true;
    			}
    			s1.push(x);
    		}
    	
    	void pop(void)
    		{
    			if(iss1)
    			{
    				while(!s1.empty())
    				{
    					s2.push(s1.top());
    					s1.pop();
    				}
    				iss1 = false;
    			}
    			s2.pop();
    		}
    	
    	int peek(void)
    		{
    			if(iss1)
    			{
    				while(!s1.empty())
    				{
    					s2.push(s1.top());
    					s1.pop();
    				}
    				iss1 = false;
    			}
    			return s2.top();
    		}
    
    	bool empty(void)
    		{
    			if(iss1)
    				return s1.empty();
    			else
    				return s2.empty();
    		}
    };

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.