当前位置:网站首页>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
边栏推荐
- *6-3 save small experts
- Use kotlin use to simplify file reading and writing
- Android & Kotlin : 困惑解答
- Browser access to swagger failed with error err_ UNSAFE_ PORT
- Redis set structure command
- OC -- Foundation -- array
- 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
- [code source] add brackets to the daily question
- 基于机智云平台的温湿度和光照强度获取
- Surfaceview flash screen (black problem)
猜你喜欢

Assignment 7.21 Joseph Ring problem and decimal conversion

Operation 7.19 sequence table

Definition of cell

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

matlab如何导入大量数据

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

关于C和OC

OC -- Foundation -- string + date and time

OC -- object replication

Data control language (DCL)
随机推荐
Install MySQL in Ubuntu and create new users
OC--包装类和处理对象
How many regions can a positive odd polygon be divided into
How to add other PHP versions to MAMP
A picture explains SQL join left and right
OC--对象复制
初识Opencv4.X----图像模板匹配
学习新技术语言流程
*6-3 save small experts
Wechat applet realizes the rotation map (automatic switching & manual switching)
matlab如何导入大量数据
Job 7.15 shell script
【数据挖掘】最近邻和贝叶斯分类器
[code source] National Railway
App的生命周期和AppleDelegate,SceneDelegate
[code source] daily question farmland Division
[code source] daily question - queue
初识Opencv4.X----图像直方图均衡
初识Opencv4.X----图像直方图绘制
[code source] score split of one question per day