码迷,mamicode.com
首页 > 其他好文 > 详细

155. Min Stack - Unsolved

时间:2017-06-10 22:33:33      阅读:248      评论:0      收藏:0      [点我收藏+]

标签:amp   tac   discuss   pytho   obj   object   .com   trie   runtime   

https://leetcode.com/problems/min-stack/#/solutions

 

Design a stack that supports push, pop, top, and retrieving the minimum element in constant time.

  • push(x) -- Push element x onto stack.
  • pop() -- Removes the element on top of the stack.
  • top() -- Get the top element.
  • getMin() -- Retrieve the minimum element in the stack.

 

Example:

MinStack minStack = new MinStack();
minStack.push(-2);
minStack.push(0);
minStack.push(-3);
minStack.getMin();   --> Returns -3.
minStack.pop();
minStack.top();      --> Returns 0.
minStack.getMin();   --> Returns -2.

 

 

 
Sol:
 
 
class MinStack(object):

    def __init__(self):
        """
        initialize your data structure here.
        """
        self.stack = []
        # set two stacks
        self.minStack = []

    def push(self, x):
        self.stack.append(x)
        if len(self.minStack) and x == self.minStack[-1][0]
        
        

    def pop(self):
        self.minStack.pop()
        return self.stack.pop()
        

    def top(self):
        """
        # :rtype: int
        """
        return self.stack[-1]
        

    def getMin(self):
        """
        # :rtype: int
        """
        
        return self.minStack[ len(self.minStack) - 1]
        


# Your MinStack object will be instantiated and called as such:
# obj = MinStack()
# obj.push(x)
# obj.pop()
# param_3 = obj.top()
# param_4 = obj.getMin()

 

 

Submission Result: Runtime Error More Details 

Runtime Error Message:
Line 44: IndexError: list index out of range
Last executed input:
["MinStack","push","push","push","getMin","top","pop","getMin"]
[[],[-2],[0],[-1],[],[],[],[]]

 

 

 

Back - up knowledge:

 

https://github.com/Premiumlab/Python-for-Algorithms--Data-Structures--and-Interviews/blob/master/Stacks%2C%20Queues%20and%20Deques/Implementation%20of%20Stack.ipynb

 

Implementation of Stack

 

Some sols:

https://discuss.leetcode.com/topic/11985/my-python-solution

 

2

https://discuss.leetcode.com/topic/37294/python-one-stack-solution-without-linklist

 

3

http://bookshadow.com/weblog/2014/11/10/leetcode-min-stack/

 

4

http://www.aichengxu.com/data/720464.htm

 

5

http://www.tuicool.com/articles/ze6BJra

 

155. Min Stack - Unsolved

标签:amp   tac   discuss   pytho   obj   object   .com   trie   runtime   

原文地址:http://www.cnblogs.com/prmlab/p/6979907.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!