「Leetcode刷题」「20」有效的括号
「Leetcode刷題」「20」有效的括號(hào)
"""找入棧條件 ,出棧條件,棧頂與下一個(gè)元素的關(guān)系,棧長(zhǎng)度來(lái)判斷"""# leetcode submit region begin(Prohibit modification and deletion)class Solution: def isValid(self, s: str) ->bool: stack = [] for char in s: if self.is_left_kuohao(char): stack.append(char) continue if self.is_right_kuohao(char): if len(stack) == 0: return False left_part = stack.pop() if self.match_kuohao(left_part,char): continue else: return False return len(stack) == 0 def is_left_kuohao(self, char): return char in "([{ " def is_right_kuohao(self, char): return char in ")]}" def match_kuohao(self, left_part, right_part): if left_part == "(" and right_part == ")": return True if left_part == "[" and right_part == "]": return True if left_part == "{ " and right_part == "}": return True return False# leetcode submit region end(Prohibit modification and deletion)-----------------------------------------------
如果是第一次閱讀文章,可以看看下面的話。
以上內(nèi)容由平時(shí)積累而成 ,盡量保證代碼與注釋合在一起 ,并未做過(guò)多篇幅的解釋 。不一定是最優(yōu)解 ,但一定是自己能看得懂的 。有疑問(wèn)的地方還請(qǐng)留言評(píng)論 。如果題干是原題 ,就沒(méi)有寫在文章中 。后續(xù)內(nèi)容會(huì)慢慢發(fā)出
歡迎大家評(píng)論,收藏和轉(zhuǎn)發(fā) 。
感謝大家的點(diǎn)贊和關(guān)注 。
先贊后看,年薪百萬(wàn)
紅帽帽,白簽簽,一起排隊(duì)做酸酸 。愿疫情早日過(guò)去 。
展開(kāi)閱讀全文投稿時(shí)間 :2022-05-22 最后更新 :2022-09-04
.jpg)
標(biāo)簽:氣流干燥設(shè)備