leetcode-hard-array- 227. Basic Calculator II

mycode  29.58%

class Solution(object):
    def calculate(self, s):
        """
        :type s: str
        :rtype: int
        """
        def deal(data,flag):
            data[:] = data[::-1]
            while data:
                if len(data) == 1:
                    break         
                a = data.pop()
                if a == '+':
                    b = data.pop()
                    c = last + b
                    data.append(c)
                elif a == '-':
                    b = data.pop()       
                    c = last - b
                    data.append(c)
                else:
                    last = a
            return data[0]
        
        data = []
        s = s.strip()
        tokens = ['*','/','+','-']
        l , r = 0, 0
        for i in s:
            if not i : continue
            if i not in tokens:
                r += 1
            else:
                data.append(int(s[l:r]))
                r += 1
                l = r
                data.append(i)
        data.append(int(s[l:r]))
       
        if '*' not in data and "/" not in data:
            return deal(data,0)
        
        res = []
        data[:] = data[::-1]
        while data:
            if '*' not in data and "/" not in data:
                break
            a = data.pop()
            if a == '*':
                b = data.pop()
                res.pop()
                c = last*b
                data.append(c)               
            elif a == '/':
                b = data.pop()
                res.pop()
                if b == 0:
                    return None
                c = last // b
                data.append(c)                  
            else:
                last = a
                res.append(a)
        return deal(res + data[::-1],1)
       

参考

import math
class Solution(object):
    
        def apply_pending_op(self, stack, pending_op, cur_int):
            if pending_op is None:
                stack.append(cur_int)
            elif pending_op == '-':
                stack.append(-cur_int)
            elif pending_op == '+':
                stack.append(cur_int)
            elif pending_op == '*':
                left = stack.pop()
                right = cur_int
                stack.append(left * right)
            elif pending_op == '/':
                left = stack.pop()
                right = cur_int
                 # bypasses integer division rounding toward negative infinity
                quo = int(float(left) / right)
                stack.append(int(quo))
            else:
                raise ValueError(pending_op)

        def calculate(self, s):
            """
            :type s: str
            :rtype: int
            """
            cur_int = 0
            stack = []
            pending_op = None
            for c in s:
                if c.isdigit():
                    cur_int = cur_int * 10 + int(c)
                elif c in ('*', '/', '+', '-'):
                    self.apply_pending_op(stack, pending_op, cur_int)
                    cur_int, pending_op = 0, c
                    
            self.apply_pending_op(stack, pending_op, cur_int)

            return sum(stack)
原文地址:https://www.cnblogs.com/rosyYY/p/11044495.html