力扣224题、227题、772题(基本计算器)

统一使用拉不拉东大佬模板

224、基本计算器I

基本思路:

括号处理使用递归

使用队列,否则会超时(不懂)

具体实现:

代码:

超时的

from collections import deque
class Solution:
    def calculate(self, s: str) -> int: 
        def helper(s):
            stack = []
            sign = '+'
            num = 0

            while len(s) > 0:
                c=s.pop(0)
                if c.isdigit():
                    num = 10*num + int(c)
                if c == '(':
                    num = helper(s)
                if (not c.isdigit() and c != ' ') or len(s) == 0:
                    if sign == '+':
                        stack.append(num)
                    elif sign == '-':
                        stack.append(-num)
                    num = 0
                    sign = c
                if c == ')':
                    break
            return sum(stack)
        return helper(list(s))

 

不超时的

from collections import deque
class Solution:
    def calculate(self, s: str) -> int:
        str=deque(s) 
        def helper(s):
            stack = []
            sign = '+'
            num = 0

            while len(s) > 0:
                c=s.popleft()
                if c.isdigit():
                    num = 10*num + int(c)
                if c == '(':
                    num = helper(s)
                if (not c.isdigit() and c != ' ') or len(s) == 0:
                    if sign == '+':
                        stack.append(num)
                    elif sign == '-':
                        stack.append(-num)
                    num = 0
                    sign = c
                if c == ')':
                    break
            return sum(stack)
        return helper(str)

 

227、基本计算器II

代码:

from collections import deque
class Solution:
    def calculate(self, s: str) -> int:
        stack = []
        sign = '+'
        num = 0
        s = deque(s)
        while len(s) > 0:
            c = s.popleft()
            if c.isdigit():
                num = 10*num + int(c)
            if (not c.isdigit() and c != ' ') or len(s) == 0:
                if sign == '+':
                    stack.append(num)
                elif sign == '-':
                    stack.append(-num)
                elif sign == '*':
                    stack[-1] = stack[-1]*num
                elif sign == '/':
                    stack[-1] = int(stack[-1]/float(num))
                num = 0
                sign = c
        return sum(stack)

 

772、基本计算器III

代码:

from collections import deque
class Solution:
    def calculate(self,s:str)->int: 
        str=deque(s)       
        def helper(s:List)->int:
            stk=[]
            num=0
            sign='+'
            while len(str)>0:
                c=s.popleft()
                if c.isdigit():
                    num=10*num+int(c)
                if c=='(':
                    num=helper(s)
                if(not c.isdigit() and c!=' ') or len(s)==0:
                    if sign=='+':
                        stk.append(num)
                    elif sign=='-':
                        stk.append(-num)
                    elif sign=='*':
                        stk[-1]=stk[-1]*num
                    elif sign=='/':
                        stk[-1]=int(stk[-1]/float(num))
                    sign=c 
                    num=0
                if c==')':
                    break
            return sum(stk)
        return helper(str)

 

上一篇:求前n项和


下一篇:ABP框架 - 缓存