Whats wrong with my solution. Its passing locally


  • 0
    S

    import java.util.Stack;
    class MyQueue {
    static Stack<Integer> left = new Stack<Integer>();
    static Stack<Integer> right = new Stack<Integer>();

    // Push element x to the back of queue.
    public void push(int x) {
        left.push(x);
    }
    
    // Removes the element from in front of queue.
    public void pop() {
        
    	while(left.size()!=1){
    		right.push(left.pop());
    	}
    	left.pop();
    	while(right.size()!=0){
    		left.push(right.pop());	
    	}
    }
    
    // Get the front element.
    public int peek() {
        int peekElement;
    	while(left.size()!=1){
    		right.push(left.pop());
    	}
    	peekElement= left.peek();
    	right.push(left.pop());
    	while(right.size()!=0){
    		left.push(right.pop());	
    	}
    	return peekElement;
    }
    
    // Return whether the queue is empty.
    public boolean empty() {
        return left.isEmpty();
        
    }
    

    }


  • 0

    For which testcase did OJ give wrong answer ? Also please don't make objects of Stack class as static members


Log in to reply
 

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