[leetcode]20. Valid Parentheses

Posted alfredsun

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[leetcode]20. Valid Parentheses相关的知识,希望对你有一定的参考价值。

少考虑了1 

 

多考虑了重复括号,和括号有数学运算符顺序:

class Solution:
    def isValid(self, s: str) -> bool:
        # 0
        lengthS = len(s)
        if lengthS == 0:
            #unbeliveable
            return True
        #1
        if lengthS == 1:
            return False
        # regular
        if "(" in s:
            s,status = self.deal(s,(,))
            if status ==False or ) in s:
                return False
            s = s.replace(0,‘‘)
        if "[" in s:
            s, status = self.deal(s, [, ])
            if status == False or ] in s:
                return False
            s = s.replace(0,‘‘)
        if "{" in s:
            s, status = self.deal(s, {, })
            if status == False or } in s:
                return False
            s = s.replace(0, ‘‘)
        if s == ‘‘:
            return True
        else:
            return False

    def deal(self,s,bracket,r_bracket):
        temp = 0
        lengthS = len(s)
        for index in range(lengthS-1):
            if s[index] == bracket:
                # ‘((()))‘
                if s[index + 1] == bracket:
                    temp += 1
                    continue

                if s[index + 1] == r_bracket:
                    if temp == 0:
                        #cut
                        s = s[:index] +00+ s[index + 2:]
                    else:
                        # ‘((()))‘
                        if s[index - temp:index + temp + 2] == (bracket * (temp + 1) + r_bracket * (temp + 1)):
                            s = s[:index - temp] +00* (temp + 1) + s[index + temp + 2:]
                            temp = 0
                        else:
                            return s, False
                else:
                    # ‘(‘ in not close
                    return s, False
        return s, True

 

Runtime: 924 ms, faster than 5.87% of Python3 online submissions for Valid Parentheses.
Memory Usage: 13.3 MB, less than 18.07% of Python3 online submissions for Valid Parentheses.
 

Submission Detail

76 / 76 test cases passed.
Status: 

Accepted

Runtime: 924 ms
Memory Usage: 13.3 MB
Submitted: 0 minutes ago

 

class Solution:
    def isValid(self, s: str) -> bool:
        # 0
        lengthS = len(s)
        if lengthS == 0:
            return True
        #1
        if lengthS == 1:
            return False
        # odd
        if lengthS %2 == 1:
            return False
        # regular
        bracket = [(),[],{}]
        while(True):
            lastlength = len(s)
            for index in range(len(s) -1):
                if s[index] + s[index +1] in bracket:
                    s = s[:index]+s[index+2:]
                    break
            if len(s) == lastlength:
                return False
            if len(s) == 0:
                return True

 

24ms:

class Solution:
    
    def isValid(self, s: str) -> bool:
        
        d = {"[":"]", "(":")", "{":"}"}

        stack = []

        for p in s:

            if p in d:
                # if an opening parenth is found, append its closing match
                stack.append(d[p])

            elif not stack or stack.pop() != p:
                return False

        return stack == []

 

 

以上是关于[leetcode]20. Valid Parentheses的主要内容,如果未能解决你的问题,请参考以下文章

leetcode20. Valid Parentheses

[LeetCode]20. Valid Parentheses

[LeetCode]20 Valid Parentheses 有效的括号

LeetCode OJ 20Valid Parentheses

Leetcode 20. Valid Parentheses

LeetCode 20. Valid Parentheses