Java, Min Stack without using stack


  • 0
    T
           List<Integer> cache = new ArrayList<Integer>();
    
            /** initialize your data structure here. */
            public MinStack() {
            }
    
            public void push(int x) {
                cache.add(new Integer(x));
            }
    
            public void pop() {
                cache.remove(cache.size() - 1);
            }
    
            public int top() {
                return cache.get(cache.size() - 1).intValue();
            }
    
            public int getMin() {
                Integer[] IntegerArray = new Integer[cache.size()];
                cache.toArray(IntegerArray);
                Arrays.sort(IntegerArray);
                return IntegerArray[0].intValue();
            }
    

Log in to reply
 

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