OOP solution, extremely consice


  • 1
    R
    public class MinStack {
        
        private StackNode node;
        /** initialize your data structure here. */
        public MinStack() {
        }
        
        public void push(int x) {
            StackNode head = new StackNode(x);
            head.min = x;
            head.next = node;
            if(node != null) {
                head.min = Math.min(x, node.min);
            }
            node = head;
        }
        
        public void pop() {
            node = node.next;
        }
        
        public int top() {
            return node.val;
        }
        
        public int getMin() {
            return node.min;
        }
        
        //customize minStack
        class StackNode {
           Integer val;
           Integer min;
           StackNode next;
           public StackNode(){}
           public StackNode(Integer val) {
               this.val = val;
           }
        }
    }

  • 0
    W

    Awesome! Thanks for your sharing.


  • 0
    S

    Nice solution, very concise. Not sure where you got the "beats 99%" from, because I am getting much lower upon multiple submissions (approx. 105-125ms runtime).


  • 0
    R

    @strmrdr I tried again, it's not beating 99% any more. Don't know what's going on. Sorry for the misunderstanding.


Log in to reply
 

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