首页 > 其他 > 详细

leetcode-hard-array- 227. Basic Calculator II

时间:2019-06-18 13:31:15      阅读:104      评论:0      收藏:0      [点我收藏+]

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)

 

leetcode-hard-array- 227. Basic Calculator II

原文:https://www.cnblogs.com/rosyYY/p/11044495.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!