easy python solution with stack


  • 0
    D
    def calPoints(self, ops):
        """
        :type ops: List[str]
        :rtype: int
        """
        s=[]
        for o in ops:
            if o=="C" and len(s)>0:
                s.pop()
            elif o=="D" and len(s)>0:
                s.append(s[-1]*2)
            elif o=="+" and len(s)>1:
                s.append(s[-1]+s[-2])
            elif o==str(int(o)):
                s.append(int(o))
        return sum(s)

Log in to reply
 

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