zoukankan      html  css  js  c++  java
  • 力扣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)
  • 相关阅读:
    python从入门到实践-5章if语句
    HiveQL:文件格式和压缩方法
    HiveQL:调优
    HiveQL:模式设计
    HiveQL:视图
    (转)配置文件.bash_profile和.bashrc的区别
    Hive-学习总结(二)
    Hive-学习总结
    MYSQL-表类型(存储引擎)的选择
    MYSQL-常用函数
  • 原文地址:https://www.cnblogs.com/zhaojiayu/p/14732034.html
Copyright © 2011-2022 走看看