讨论/题目交流/关于第227题超过时间限制的问题/
关于第227题超过时间限制的问题

各位大佬好,为什么我在我跑第227题(基本计算器II)的时候告诉我第109个用例超出时间限制,但是点开之后并不告诉我该用例是什么。有哪位大佬知道这个用例是什么或者程序哪里有bug吗?谢谢了。

代码片段

class Solution:
    def Fun(self, nums:str, begin):
        res = 0
        while(begin < len(nums) and 0 <= ord(nums[begin]) - ord('0') <=9):
            res = res * 10 + ord(nums[begin]) - ord('0')
            begin += 1
        return res, begin - 1
    
    def Split(self, nums: str):
        res = []
        i = 0
        while(i < len(nums)):
            if nums[i] == '*' or nums[i] == '/':
                res.append(nums[i])
            elif nums[i] == '+' or nums[i] == '-':
                if i != 0:
                    res.append(nums[i])
                else:
                    zz,point = self.Fun(nums, i+1)
                    if nums[i] == '-':
                        res.append(-zz)
                    else:
                        res.append(zz)
                    i = point
            else:
                zz,point = self.Fun(nums,i)
                res.append(zz)
                i = point
            i += 1
        return res
    
    def HighLevel(self, cal):
        point = 0
        while(point < len(cal)):
            if cal[point] == '*' or cal[point] == '/':
                num1 = cal[point-1]
                if cal[point] == '*':
                    num1 *= cal[point+1]
                else:
                    quotient = num1 // cal[point+1]
                    if quotient < 0 and num1 != quotient * cal[point+1]:
                        quotient += 1
                    num1 = quotient
                cal.pop(point)
                cal.pop(point)
                cal[point-1] = num1
                point -= 1
            point += 1
        return cal
    
    def LowLevel(self, cal):
        while(len(cal) != 1):
            operator = cal[1]
            num1 = cal[0]
            if operator == '+':
                num1 += cal[2]
            else:
                num1 -= cal[2]
            cal.pop(0)
            cal.pop(0)
            cal[0] = num1
        return cal[0]
    
    def calculate(self, s: str) -> int:
        tmp = "".join(map(str,s.split()))
        if (len(tmp) == 0):
            return 0
        #首先把每个字符拆分放到cal中
        cal = self.Split(tmp)
        #先算乘除
        cal = self.HighLevel(cal)
        #再算加减
        return self.LowLevel(cal)

problem.png

展开讨论
Chizuru Kagura发起于 2020-04-02
共 1 个讨论

image.png
这里可以查看数据
不过由于是最后一组数据
所以我猜是单纯的超时了