当前位置:网站首页>[leetcode simple] 20. Valid brackets stack
[leetcode simple] 20. Valid brackets stack
2022-07-24 07:20:00 【qmkn】


solution : Stack
As soon as you see the problem of bracket matching , The first idea is to use stack to realize , Scan the string , If the current character is ’ ( ’ ’ [ ’ ’ { ', Then push it onto the stack , Otherwise pop up the top of the stack , Judge whether the character at the top of the stack matches the current character , If it matches, continue to judge the next character , If there is no match, return false. Attention should be paid to , There are nine endings of the first character ’ ) ’ ’ ] ’ ’ } ’ The situation of , This situation obviously does not match , So before popping the character at the top of the stack, you need to judge whether the current stack is empty , If it is empty , Obviously, the string is invalid , return false
Besides , There are still things like “[[()” The situation of , Therefore, after scanning the string, you need to judge whether the current stack is empty , Empty indicates that the brackets match successfully , The string is valid , return true; Non empty means that the string is invalid , return false
The code is as follows :
class Solution {
public:
bool isValid(string s) {
stack<char> sta;
for(int i=0;i<s.length();i++){
if(s[i]=='(' || s[i]=='[' || s[i]=='{'){
sta.push(s[i]);
}
else{
if(sta.empty()) return false;
char temp=sta.top();
sta.pop();
if((temp=='('&&s[i]==')') || (temp=='['&&s[i]==']') || (temp=='{'&&s[i]=='}')){
continue;
}else{
return false;
}
}
}
if(sta.empty()) return true;
else return false;
}
};
Use in the problem solution given by Li Kou map Make the process of judging whether it matches more concise , The code is as follows for reference :
class Solution {
public:
bool isValid(string s) {
int n = s.size();
if (n % 2 == 1) {
return false;
}
unordered_map<char, char> pairs = {
{
')', '('},
{
']', '['},
{
'}', '{'}
};
stack<char> stk;
for (char ch: s) {
if (pairs.count(ch)) {
if (stk.empty() || stk.top() != pairs[ch]) {
return false;
}
stk.pop();
}
else {
stk.push(ch);
}
}
return stk.empty();
}
};
边栏推荐
猜你喜欢

第一部分—C语言基础篇_11. 综合项目-贪吃蛇

【时序逻辑电路】——寄存器

我的创作纪念日
![[leetcode] 11. Container with the most water - go language solution](/img/42/3a1839dd768a5f02dc2acb5bd66438.png)
[leetcode] 11. Container with the most water - go language solution

Three implementation methods of single sign on

QoS服务质量三DiffServ模型报文的标记及PHB

单点登录的三种实现方式

深度学习二三事-回顾那些经典卷积神经网络

QoS quality of service 4 traffic regulation of QoS boundary behavior

Decompress the anchor and enjoy 4000w+ playback, adding a new wind to the Kwai food track?
随机推荐
Jenkins 详细部署
Who can stand it when the project goes online
电子商务时代,企业社交电商转型要做什么?
Upload pictures Base64
Hackingtool of security tools
Aggregated new ecological model - sharing purchase, membership and reward system
Redis master-slave mechanism
全国职业院校技能大赛网络安全B模块 缓冲区溢出漏洞
第二部分—C语言提高篇_4. 二级指针
17. What is the situation of using ArrayList or LinkedList?
B. Also Try Minecraft
Vulnhub DC1
Riotboard development board series notes (IX) -- buildreoot porting matchbox
UNI-APP_小程序或h5页面背景音乐的播放与暂停
单场GMV翻了100倍,冷门品牌崛起背后的“通用法则”是什么?
Redis fragment cluster
MongoDB应用场景及选型(海量数据存储选型)
Can recursion still play like this? Recursive implementation of minesweeping game
Talk about your thoughts about the future
Bookkeeping app: xiaoha bookkeeping 2 - production of registration page