码迷,mamicode.com
首页 > 其他好文 > 详细

[leetcode]20. Valid Parentheses

时间:2019-05-24 19:32:32      阅读:110      评论:0      收藏:0      [点我收藏+]

标签:close   state   temp   pass   open   ase   ble   ast   tac   

少考虑了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

标签:close   state   temp   pass   open   ase   ble   ast   tac   

原文地址:https://www.cnblogs.com/alfredsun/p/10919547.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!