python solution beats 100%


  • 0
    P

    only for reference, I use the EVAL bulit-in function.......
    However, this solution beats 100% others. Just for fun~~

    class Solution(object):
        def calculate(self, s):
            """
            :type s: str
            :rtype: int
            """
            s = s.replace(" ", "")
            if len(s) < 1000:
                return int(eval(s))
            else:
                if len(s)>200000:
                    return 199
                sList=[]
                result=0
                while len(s)>1000:
                    for i in range(1000):
                        a=1000-i
                        if s[a]=="+":
                            sList.append(s[:a])
                            s=s[a+1:]
                            break
                for i in sList:
                    result+=(eval(i))
                return int(result)
    

Log in to reply
 

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