Python3给定⼀个只包括(,),{,},[,]的字符串,判断字jay z好听的歌
anderson cooper符串是否有效。
buffet是什么意思
题⽬:
给定⼀个只包括 '(',')','{','}','[',']' 的字符串,判断字符串是否有效。
英语短语
temp有效字符串需满⾜:
左括号必须⽤相同类型的右括号闭合。
左括号必须以正确的顺序闭合。
注意空字符串可被认为是有效字符串。
⽰例 1:
输⼊: "()"
输出: true
代码:
class Solution:
def isValid(lf, s: str) -> bool:
len_s=len(s)
if len_s%2 !=0:
new stylereturn Fal爱心词典
hanukkahif len_s==0:
return True
#利⽤进栈出栈的思想
str_dict={'(':')','[':']','{':'}'}
stacked=[]
for i in range(len_s):
#判断stacked是否为空,以及stacked的最后⼀个元素是否存在于字典的关键字之中heels什么意思
#判断字符串中的下⼀个字符是否与stacked中存储的最后⼀个元素相等,相等则删除stacked中的最后⼀个元素
#不相等则存⼊stacked之中,⽤于接下来的判断
if stacked and stacked[-1] in str_dict.keys() and s[i]==str_dict[stacked[-1]]:
stacked.pop()
el:
环太平洋 音乐
stacked.append(s[i])
#若输⼊为有效字符串则最终stacked中的左右元素将会被完全删除
if stacked==[]:
return True
el:
return Fal