当前位置:网站首页>Bracket matching problem
Bracket matching problem
2022-07-25 09:45:00 【Frost Creek】
The problem is that , We enter a set of strings , These strings are composed of parentheses (,), brackets [,], Curly braces {,} form . The combination is arbitrary . Such as “([]) ",“[()]", "[[({}))]]”“{}(){}[]” And so on are considered to be correct , and “[)","[){","()[}" And so on are considered to be wrong .
To sum up, that is :
Opening parentheses must be closed with closing parentheses of the same type .
The left parenthesis must be closed in the correct order
Code implementation :
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
Example :
str1="([}"print(judge(str1))str2="([{}])"print(judge(str2))
result :
FalseTrue
边栏推荐
猜你喜欢
随机推荐
Data control language (DCL)
App的生命周期和AppleDelegate,SceneDelegate
解决esp8266无法连接手机和电脑热点的问题
Wechat applet realizes the rotation map (automatic switching & manual switching)
初识Opencv4.X----图像模板匹配
matlab绘图|坐标轴axis的一些常用设置
How to convert object data into arrays
yolov5实现小数据集的目标检测--kolektor缺陷数据集
Constant power wireless charging based on stm32
Singleton mode
【数据挖掘】第四章 分类任务(决策树)
Some operations of main function
OC--Foundation--集合
基于stm32的恒功率无线充电
Swagger2 shows that there is a problem with the get interface, which can be solved with annotations
Dream set sail (the first blog)
括号匹配问题
TCP network application development process
初识Opencv4.X----均值滤波
[code source] daily question tree




![[code source] National Railway](/img/33/ea786a10417487a2426be3a28d28aa.jpg)




