1
我不明白何時這個函數將返回'真'當一個輸入字符串匹配括號?它在哪裏返回'真'?爲什麼這個python函數返回true?
def balance_check(s):
if len(s)%2 !=0:
return False
opening = set('([{')
matches = set([('(',')'),('[',']'),('{','}')])
stack =[]
for paren in s:
if paren in opening:
stack.append(paren)
else:
if len(stack) == 0:
return False
last_open = stack.pop()
if (last_open,paren) not in matches:
return False
return len(stack) == 0
res=balance_check('[]')
您可能能夠通過剛剛經歷[pythontutor(HTTP運行它回答你自己的問題:// pythontutor.com/visualize.html#) –