def is_balanced(input_str): s = list()
for ch in input_str:
if ch == '(':
s.append(ch)
if ch == ')':
if not s:
return False
s.pop()
return not s
誰能解釋“if ch == ')':”行之后的真實情況?
uj5u.com熱心網友回復:
if not s:
檢查串列是否為空,如果串列為空,則執行里面的條件。
假設您提供())()()輸入,因此:
-> 它將首先添加(到的第 0 個索引s
-> 對于第一個索引(即)):它將首先檢查是否s為空,就像我們(在這里一樣,s所以if條件不會執行,然后它繼續從串列中彈出值。(注意現在串列是空的)
-> 在進入第二個索引時(即)):它將首先檢查串列是否為空,因為串列為空,它將回傳False
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/420082.html
標籤:
上一篇:python包中的物件是什么?
