runtime beats 23.91% of python submissions


  • 0
    C

    class Solution(object):
    def isValid(self, s):
    """
    :type s: str
    :rtype: bool
    """
    stack = []
    for c in s:
    if c == '(' or c == '[' or c == '{':
    stack.append(c)
    if c == ')':
    if stack and stack[-1] == '(':
    stack.pop(-1)
    continue
    else:
    return False
    if c == ']':
    if stack and stack[-1] == '[':
    stack.pop(-1)
    continue
    else:
    return False
    if c == '}':
    if stack and stack[-1] == '{':
    stack.pop(-1)
    continue
    else:
    return False
    return not stack


  • 0
    C

    @CGQAQ said in runtime beats 23.91% of python submissions:

    class Solution(object):

    • def isValid(self, s):
        """
        :type s: str
        :rtype: bool
        """
        stack = []
        for c in s:
            if c == '(' or c == '[' or c == '{':
                stack.append(c)
            if c == ')':
                if stack and stack[-1] == '(':
                    stack.pop(-1)
                    continue
                else:
                    return False
            if c == ']':
                if stack and stack[-1] == '[':
                    stack.pop(-1)
                    continue
                else:
                    return False
            if c == '}':
                if stack and stack[-1] == '{':
                    stack.pop(-1)
                    continue
                else:
                    return False
        return not stack

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.