solution by python


  • 0
    H

    class Solution(object):
    def isValid(self, s):
    """
    :type s: str
    :rtype: bool
    """
    stack=[]
    for i in s:
    if i in ['(','[','{']:
    stack.append(i)
    else:
    if len(stack)==0:
    flag=0
    break
    else:
    R=stack.pop()
    if i==']'and R=='[':
    flag=1
    elif i=='}'and R=='{':
    flag=1
    elif i==')'and R=='(':
    flag=1
    else:
    flag=0
    break
    if len(stack)!=0:
    flag=0
    if flag==1:
    return True
    else:
    return False


Log in to reply
 

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