• 代码随想录算法训练营第十一天|20. 有效的括号,1047. 删除字符串中的所有相邻重复项,150. 逆波兰表达式求值


    今天是代码随想录算法训练营第十一天;
    写了3道力扣:20. 有效的括号,1047. 删除字符串中的所有相邻重复项,150. 逆波兰表达式求值

    1. 有效的括号:
    # 方法一,仅使用栈,更省空间
    class Solution:
        def isValid(self, s: str) -> bool:
            stack = []
            
            for item in s:
                if item == '(':
                    stack.append(')')
                elif item == '[':
                    stack.append(']')
                elif item == '{':
                    stack.append('}')
                elif not stack or stack[-1] != item:
                    return False
                else:
                    stack.pop()
            
            return True if not stack else False
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    1. 删除字符串中的所有相邻重复项
    # # 方法一,使用栈
    # class Solution:
    #     def removeDuplicates(self, s: str) -> str:
    #         res = list()
    #         for item in s:
    #             if res and res[-1] == item: # 如果栈非空 且 栈顶元素等于传入进来的item
    #                 res.pop() # 那么就进行消除操作
    #             else:
    #                 res.append(item)
    #         return "".join(res)  # 字符串拼接
    
    
    # 此题还可以用双指针的方法
    # 方法二,使用双指针模拟栈,如果不让用栈可以作为备选方法。
    class Solution:
        def removeDuplicates(self, s: str) -> str:
            res = list(s)
            slow = fast = 0
            length = len(res)
    
            while fast < length:
                # 如果一样直接换,不一样会把后面的填在slow的位置
                res[slow] = res[fast]
                
                # 如果发现和前一个一样,就退一格指针
                if slow > 0 and res[slow] == res[slow - 1]:
                    slow -= 1
                else:
                    slow += 1
                fast += 1
                
            return ''.join(res[0: slow])
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    1. 逆波兰表达式求值
    from operator import add, sub, mul
    
    class Solution:
        op_map = {'+': add, '-': sub, '*': mul, '/': lambda x, y: int(x / y)}
        
        def evalRPN(self, tokens: List[str]) -> int:
            stack = []
            for token in tokens:
                if token not in {'+', '-', '*', '/'}:
                    stack.append(int(token))
                else:
                    op2 = stack.pop()
                    op1 = stack.pop()
                    stack.append(self.op_map[token](op1, op2))  # 第一个出来的在运算符后面
            return stack.pop()
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
  • 相关阅读:
    EasyExcel使用实体类进行读操作和写操作
    【QT】事件分发器/事件过滤器/事件处理的介绍和使用
    记录小技巧--前端等所有的请求都结束了再刷新页面,button对齐input
    【GPU并行计算】利用OpenCL&OpenCLUtilty进行GPU并行计算
    依赖的aar包跟项目jdk版本不一致问题
    git 的使用以及如何解决git冲突问题
    技术对接51
    光学教程考试练习题及答案
    【项目实战:核酸检测平台】第三章 利其器
    计算机的前世今生
  • 原文地址:https://blog.csdn.net/qq_42839893/article/details/132922686