網(wǎng)站首頁 編程語言 正文
python:實(shí)現(xiàn)balanced parentheses平衡括號表達(dá)式算法(附完整源碼)
作者:全棧技術(shù)博客 更新時(shí)間: 2022-07-21 編程語言python:實(shí)現(xiàn)balanced parentheses平衡括號表達(dá)式算法
from .stack import Stack
def balanced_parentheses(parentheses: str) -> bool:
stack: Stack[str] = Stack()
bracket_pairs = {"(": ")", "[": "]", "{": "}"}
for bracket in parentheses:
if bracket in bracket_pairs:
stack.push(bracket)
elif bracket in (")", "]", "}"):
if stack.is_empty() or bracket_pairs[stack.pop()] != bracket:
return False
return stack.is_empty()
if __name__ == "__main__":
from doctest import testmod
testmod()
examples = ["((()))", "((())", "(()))"]
print("Balanced parentheses demonstration:\n")
for example in examples:
not_str = "" if balanced_parentheses(example) else "not "
print(f"{example} is {not_str}balanced")
原文鏈接:https://blog.csdn.net/it_xiangqiang/article/details/125898454
相關(guān)推薦
- 2022-04-09 git拉取遠(yuǎn)程分支到本地分支
- 2022-07-15 C++?DLL動(dòng)態(tài)庫的創(chuàng)建與調(diào)用(類庫,隱式調(diào)用)_C 語言
- 2022-06-01 C++程序內(nèi)存棧區(qū)與堆區(qū)模型案例分析_C 語言
- 2022-09-09 python定義函數(shù)如何判斷年份是否為閏年_python
- 2022-08-17 python熱力圖實(shí)現(xiàn)的完整實(shí)例_python
- 2022-10-19 React封裝彈出框組件的方法_React
- 2022-09-15 python實(shí)現(xiàn)簡單的計(jì)算器功能_python
- 2023-11-21 MAC 打開不開第三方應(yīng)用XXX can’t be opened because the ident
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運(yùn)行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運(yùn)算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯(cuò)誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實(shí)現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡單動(dòng)態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊(duì)列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支