当前位置:网站首页>括号匹配问题
括号匹配问题
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
边栏推荐
- *7-2 CCF 2015-09-2 date calculation
- 【深度学习】自编码器
- UI——无限轮播图和分栏控制器
- 初识Opencv4.X----图像卷积
- [code source] add brackets to the daily question
- OC--继承和多态and指针
- *7-1 CCF 2015-09-1 sequence segmentation
- Stm32+hc05 serial port Bluetooth design simple Bluetooth speaker
- How to customize the title content of uni app applet (how to solve the problem that the title of applet is not centered)
- Flex layout syntax and use cases
猜你喜欢

UI - infinite rotation chart and column controller

学习 Redis linux 安装Redis

@3-2 optimal threshold of CCF 2020-12-2 final forecast

初识Opencv4.X----方框滤波

Assignment 7.21 Joseph Ring problem and decimal conversion

Swagger2 shows that there is a problem with the get interface, which can be solved with annotations

Swift创作天气APP

基于树莓派4b的传感器数据可视化实现

Flex 布局语法与用例

语音聊天app源码-钠斯网络源码出品
随机推荐
作业7.15 shell脚本
Redis set 结构命令
初识Opencv4.X----图像直方图匹配
浏览器访问swagger失败,显示错误ERR_UNSAFE_PORT
[code source] National Railway
Assignment 7.21 Joseph Ring problem and decimal conversion
OC--Foundation--集合
Learning new technology language process
【深度学习】卷积神经网络
如何将Jar包部署到服务器,注:启动命令有无nohup有很大关系
Voice chat app source code - produced by NASS network source code
如何将其他PHP版本添加到MAMP
Basic network knowledge
OC--对象复制
UI - infinite rotation chart and column controller
OC--Foundation--字符串+日期和时间
Swagger2显示get接口有问题,加注解就能解决
Learn redis Linux and install redis
Redis string 结构命令
初识Opencv4.X----为图像添加高斯噪声