当前位置:网站首页>Leetcode 20. 有效的括号
Leetcode 20. 有效的括号
2022-07-17 15:36:00 【LuZhouShiLi】
Leetcode 20. 有效的括号
题目

思路
首先遍历完所有的左括号,将每一个左括号对应的右括号全部入栈,然后比较当前s[i]和栈顶的右括号是否相同即可。
代码
class Solution {
public:
bool isValid(string s) {
stack<int> st;
for(int i = 0; i < s.size(); i++)
{
// 匹配左括号的时候 右括号先入栈 等左括号全部遍历完毕之后,就只需要比较当前元素和栈顶是否相等就可以
if(s[i] == '(')
{
st.push(')');
}
else if(s[i] == '{')
{
st.push('}');
}
else if(s[i] == '[')
{
st.push(']');
}
else if(st.empty() == true || st.top() != s[i])
{
// 走到这里 说明栈中没有字符了 或者s[i]是右括号 那么直接返回错误
return false;
}
else
{
st.pop();// st.top() 与 s[i]相等 栈弹出元素
}
}
// 看一下栈是不是空 不为空 说明还有左括号没有被匹配
return st.empty();
}
};
边栏推荐
- Leetcode 1304. N different integers with zero and
- 02-2. Default parameters, function overloading, reference, implicit type conversion, about error reporting
- Research and implementation of 5g network Slicing Based on AI intelligent correlation technology
- TCP拥塞控制详解 | 7. 超越TCP
- 2022.07.13 summer training personal qualifying (VIII)
- STL string输入输出重载
- 夢想CMS 前臺搜索SQL注入
- Nature子刊 | 地下水固碳速率与寡营养海洋系统固碳速率相近
- 梦想CMS 前台搜索SQL注入
- Will causal learning open the next generation of AI? Chapter 9 Yunji datacanvas officially released the open source project of ylarn causal learning
猜你喜欢

Use native JS to realize the function of selecting all buttons, which is simple and clear

From prediction to decision-making, Chapter 9 Yunji datacanvas launched the ylearn causal learning open source project

【二叉树】之力扣牛客必刷题
![[embedded unit test] construction of C language unit test framework](/img/d4/ba7e5ffa1a782c2478d1f6682f2637.png)
[embedded unit test] construction of C language unit test framework

解决:code ERESOLVE:ERESOLVE could not resolve 的报错问题

HCIP(4)

Flink

TCP congestion control details | 7 Surpass TCP
![[PostgreSQL] PostgreSQL 15 optimizes distinct](/img/7c/89d05171902dd88bd2b5c352c3614f.png)
[PostgreSQL] PostgreSQL 15 optimizes distinct

LeetCode_ 77_ combination
随机推荐
Redis分布式緩存-Redis集群
getchar()
MySQL cannot be started? Relevant components missing? System upgrade? Component mismatch? Start reinstalling MySQL
The adaptation of go language under windows10:vscode
The concept of binary tree and three traversal methods (C language)
[wechat applet] use a thousand hand float - rollback
function/symbol ‘pango_context_set_round_glyph_positions‘ not found in library ‘libpango-1.0.so.0‘x
[embedded unit test] construction of C language unit test framework
Wechat applet cloud development 1 - Database
02-2. Default parameters, function overloading, reference, implicit type conversion, about error reporting
High performance IO framework library libevent (III): overview of libevent framework functions
Region performance tuning
Two misunderstandings of digital transformation
Fundamentals of scala (3): operators and process control
Robot development -- common simulation software tools
024.static and final use traps continued
项目建设,谋事在人,成事亦在人!
02-3、指針和引用的區別
C# .NET 云南农信国密签名(SM2)简要解析
Developing those things: how to solve the problem of long-time encoding and decoding of RK chip video processing?