首页
学习
活动
专区
圈层
工具
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

数据结构算法操作试题(C++/Python)——有效的括号

1. 题目

leetcode 链接:https://leetcode-cn.com/problems/valid-parentheses/submissions/

2. 解答

stack python: 44 ms, 10.8 MB

代码语言:javascript
复制
class Solution(object):
    def isValid(self, s):
        """
        :type s: str
        :rtype: bool
        """
        parentheses_dict = {")": "(", "}": "{", "]": "["}
        
        stack_list = []
        for i in range(len(s)):
            if s[i] in parentheses_dict:
                if stack_list and stack_list[-1] == parentheses_dict[s[i]]:
                    stack_list = stack_list[:-1]
                else:
                    return False
            else:
                stack_list.append(s[i])
        if stack_list: return False
        return True

其他方法看 leetcode 链接 评论区~

下一篇
举报
领券