?
输出为
True
False
def isValid(s: str) -> bool:
dc = {'(': ')', '{': '}', '[': ']'}
while len(s) > 0:
if len(s) % 2 == 1:
return False
elif dc[s[0]] in s:
s = s.strip(dc[s[0]])
s = s.strip(s[0])
else:
return False
return True
s1 = '()'
s2 = "()[]{}"
print(isValid(s1))
print(isValid(s2))
这是一个判断括号序列是否合法的函数,使用了栈的思想。但是代码中有一些问题:
%
,而不是除法运算符 /
。strip
方法。下面是修改后的代码:
def isValid(s: str) -> bool:
stack = []
dc = {'(': ')', '{': '}', '[': ']'}
for c in s:
if c in dc:
stack.append(c)
elif not stack or dc[stack.pop()] != c:
return False
return not stack
s1 = '()'
s2 = "()[]{}"
print(isValid(s1))
print(isValid(s2))
我需要更具体的问题和上下文信息才能给出具体的解决方案,请您提供更多信息。参考资料中提供了一些关于聊天机器人以及数据清洗、词向量等方面的内容,但具体问题需要更具体的描述。
已解决!!!
python str字符串的strip方法只能是去除首尾,应该用replace进行替换。(存在一些问题,但是不影响输出)
函数整体不适合"([)]"的输入。