当前位置:网站首页>括号匹配问题
括号匹配问题
2022-07-25 09:22:00 【霜溪】
问题是这样的,我们输入一组字符串,这些字符串是由小括号(,),中括号[,],大括号{,}组成。组合是任意的。诸如“([]) ",“[()]", "[[({}))]]”“{}(){}[]”等等被认为是正确的,而“[)","[){","()[}"等等则被认为是错误的。
总结起来也就是:
左括号必须用相同类型的右括号闭合。
左括号必须以正确的顺序闭合
代码实现:
def judge(s):L=[]index=0Balance=Trueopenss="([{"closess=")]}"while index<len(s) and Balance:closes=s[index]if closes in "({[":L.append(s[index])else:if len(L)==0:Balance=Falseelse:opens=L.pop()Balance=openss.index(opens)==closess.index(closes)index=index+1if len(L)==0 and Balance:return Trueelse:return False
示例:
str1="([}"print(judge(str1))str2="([{}])"print(judge(str2))
结果:
FalseTrue
边栏推荐
- 基于机智云平台的温湿度和光照强度获取
- laravel 调用第三方 发送邮件 (php)
- The jar package has been launched on Alibaba cloud server and the security group has been opened, but postman still can't run. What should we do
- 【代码源】每日一题 国家铁路
- Swift创作天气APP
- 最短路问题 Bellman-Ford(单源最短路径)(图解)
- @2-1 safety index predicted by CCF at the end of December 1, 2020
- Learning new technology language process
- ~1 CCF 2022-06-2 treasure hunt! Big adventure!
- 【代码源】 每日一题 素数之欢(bfs)
猜你喜欢
随机推荐
Operation 7.19 sequence table
OC -- first acquaintance
Redis list structure command
Jar包在阿里云服务器起起来了,安全组也开通了,但postman仍跑不通怎么办
A picture explains SQL join left and right
自定义Dialog 实现 仿网易云音乐的隐私条款声明弹框
@3-2 optimal threshold of CCF 2020-12-2 final forecast
单例模式(Singleton)
Indexes, views and transactions of MySQL
用kotlin怎么写Android切换界面
OC--Foundation--字典
初识Opencv4.X----为图像添加椒盐噪声
Redis set 结构命令
@4-1 CCF 2020-06-1 linear classifier
OC--包装类和处理对象
为什么要使用JSON.stringify()和JSON.parse()
初识Opencv4.X----方框滤波
CoreData存储待办事项
初识Opencv4.X----图像直方图绘制
*6-3 save small experts


![[GPLT] 2022 大众情人(floyd)](/img/30/c96306ca0a93f22598cec80edabd6b.png)






