Why my code get Time Limit Exceeded?


  • 0
    W

    class MinStack {
    private StackNode top = null;

    // inner class StackNode
    private class StackNode{
        private int value;
        private StackNode next;
        
        private StackNode(int value, StackNode next){
            this.value = value;
            this.next = next;
        }
    }
    
    public void push(int x) { //error occurs here when push a big amount of values
        StackNode newNode = new StackNode(x, top);
        top = newNode;
    }
    
    public void pop() {
        if(top != null)
            top = top.next;
    }
    
    public int top() {
        if(top != null){
            StackNode tempNode = top;
            return tempNode.value;
        }
        return Integer.MIN_VALUE;
    }
    
    public int getMin() {
        StackNode tempNode = top;
        int tempMin = Integer.MAX_VALUE;
        if(top != null) tempMin = top.value;
        while(tempNode != null){
            tempMin = Math.min(tempMin, tempNode.value);
            tempNode = tempNode.next;
        }
        return tempMin;
    }
    

    }


  • 0
    C

    I have some C++ code and like this, and i get the error is "Memory Limit Exceeded"

    class Node{
    Node *prenode;
    int numx;
    public:
    Node(int x,Node *pre)
    {
    numx=x;
    prenode=pre;
    }
    };

    class MinStack {
    Node *topnode=NULL;
    public:
    void push(int x) {
    topnode=new Node(x,topnode);
    }
    ...
    ...
    }


  • 0
    S

    Pay attention to "Writing code? Select all code then click on the {} button to preserve code formatting.” above text editor.


Log in to reply
 

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