0020.ValidParentheses

from typing import List

#--------input---------
s = ['{[([])]}']
s1 = '[)'
s2 = '()'
s3 = ']'

#----------------------------fail--------------------------------
#--fail1:[)不能过...
# def isVld(s: str) -> bool:
#     ram = []
#     for x in s:
#         if x == '{' or x == '[' or x == '(':
#             ram.append(x)
#         else:
#             ram.pop()
#     if ram:
#         return(False)
#     else:
#         return(True)

#--fail2:]不能过,ram为空时[-1]越界
# def isVld(s: str) -> bool:
#     ram = []
#     for x in s:
#         print(x,'\n')
#         if x == '{' or x == '[' or x == '(':
#             ram.append(x)
#         elif x == '}' and ram[-1] == '{' :
#             ram.pop()
#         elif x == ']' and ram[-1] == '[' :
#             ram.pop()
#         elif x == ')' and ram[-1] == '(' :
#             ram.pop()
#         else:
#             return(False)
#     if ram:
#         return(False)
#     else:
#         return(True)

#----------------------------success--------------------------------
#----try1: 执行用时:36 ms,内存消耗:13.6 MB,代码看上去很笨,不影响实用
# def isVld(s: str) -> bool:
#     ram = []
#     for x in s:
#         if x == '{' or x == '[' or x == '(':
#             ram.append(x)
#         else:
#             if(len(ram)>0):
#                 if x == '}' and ram[-1] == '{' :  #字符串也可用[-1]表示最后一个字符,注意字符串为空时会报错
#                     ram.pop()
#                 elif x == ']' and ram[-1] == '[' :
#                     ram.pop()
#                 elif x == ')' and ram[-1] == '(' :
#                     ram.pop()
#                 else:
#                     return(False)
#             else:
#                     return(False)
#     if ram:
#         return(False)
#     else:
#         return(True)

#----try2: 执行用时:36 ms,内存消耗:13.7 MB,代码看上去有点笨,不影响实用
# def isVld(s: str) -> bool:
#     if not s:
#         return True
#     ram = []
#     for x in s:
#         if x == '{' or x == '[' or x == '(':
#             ram.append(x)
#         else:
#             if ram:
#                 if x == '}' and ram.pop() != '{' :  
#                     return False
#                 elif x == ']' and ram.pop() != '[' :
#                     return False
#                 elif x == ')' and ram.pop() != '(' :
#                     return False
#             else:
#                 return False
#     if ram:
#         return False
#     else:
#         return True

#----try best: 执行用时:40 ms,内存消耗:13.7 MB,代码看上去最简洁,同时加入如果为奇数个符号直接返回False
#时间复杂度 O(N)
#空间复杂度 O(N)
def isVld(s: str) -> bool:
    if not s:  #判断字符串、数组是否为空,可以直接用这种方式,不必用len(s)==0
        return True
    if len(s)%2 == 1:  #如果为奇数个符号直接返回False
        return False
    ram = []
    for x in s:
        if x == '{':
            ram.append('}')
        elif x == '[':
            ram.append(']')
        elif x == '(':
            ram.append(')')
        elif not ram or x != ram.pop():
            return False
    if ram:
        return False
    else:
        return True


#--------output---------
print(isVld(s))
print(isVld(s1))
print(isVld(s2))
print(isVld(s3))

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值