当前位置:网站首页>LeetCode 20、有效的括号
LeetCode 20、有效的括号
2022-04-23 20:23:00 【亡于灬】
20、有效的括号
1)题目描述
给定一个只包括 '(',')','{','}','[',']' 的字符串 s ,判断字符串是否有效。
有效字符串需满足:
- 左括号必须用相同类型的右括号闭合。
- 左括号必须以正确的顺序闭合。
示例 1:
输入:s = "()"
输出:true
示例 2:
输入:s = "()[]{}"
输出:true
示例 3:
输入:s = "(]"
输出:false
示例 4:
输入:s = "([)]"
输出:false
示例 5:
输入:s = "{[]}"
输出:true
提示:
1 <= s.length <= 10^4s仅由括号'()[]{}'组成
2)分析
利用栈的先入后出特性,使用栈来解决。
- 括号完成配对,那么字符串一定是偶数长度;
- 遍历字符串,若是左括号,则向栈中压入相应的右括号;
- 若是右括号,判断栈顶字符是否与其相等,若不相等,返回
false; - 遍历结束,若栈为空,返回
true,否则返回false。
3)C++代码
class Solution {
public:
bool isValid(string s) {
if(s.length()%2)
return false;
stack<int> stk;
for(int i=0;i<s.length();i++){
if(s[i]=='(')
stk.push(')');
else if(s[i]=='[')
stk.push(']');
else if(s[i]=='{')
stk.push('}');
else if(stk.empty()||stk.top()!=s[i])
return false;
else
stk.pop();
}
return stk.empty();
}
};
版权声明
本文为[亡于灬]所创,转载请带上原文链接,感谢
https://blog.csdn.net/qq_38342510/article/details/124360469
边栏推荐
- 波场DAO新物种下场,USDD如何破局稳定币市场?
- Introduction to link database function of cadence OrCAD capture CIS replacement components, graphic tutorial and video demonstration
- The second method of file upload in form form is implemented by fileitem class, servletfileupload class and diskfileitemfactory class.
- ABAQUS script email auto notification
- The market share of the financial industry exceeds 50%, and zdns has built a solid foundation for the financial technology network
- Local call feign interface message 404
- redis 分布式锁
- How to do product innovation—— Exploration of product innovation methodology I
- PCL点云处理之基于PCA的几何形状特征计算(五十二)
- PCL点云处理之直线与平面的交点计算(五十三)
猜你喜欢

Scrapy教程 - (2)寫一個簡單爬蟲
![[latex] 5 how to quickly write out the latex formula corresponding to the formula](/img/1f/3c5a332ce1d6852dde38040faea5bf.png)
[latex] 5 how to quickly write out the latex formula corresponding to the formula

Leetcode dynamic planning training camp (1-5 days)

Recognition of high-speed road signs by Matlab using alexnet
![[stack and queue topics] - sliding window](/img/65/a2ce87f1401d7a28d4cce0cc85175f.png)
[stack and queue topics] - sliding window

Devexpress 14.1 installation record

Notes of Tang Shu's grammar class in postgraduate entrance examination English

PIP installation package reports an error. Could not find a version that satisfies the requirement pymysql (from versions: none)

Some basic knowledge of devexpress report development

Customize timeline component styles
随机推荐
JDBC database addition, deletion, query and modification tool class
Unity 模型整体更改材质
16MySQL之DCL 中 COMMIT和ROllBACK
R language survival package coxph function to build Cox regression model, ggrisk package ggrisk function and two_ Scatter function visualizes the risk score map of Cox regression, interprets the risk
Redis cache penetration, cache breakdown, cache avalanche
【问题解决】‘ascii‘ codec can‘t encode characters in position xx-xx: ordinal not in range(128)
Redis installation (centos7 command line installation)
Handwritten Google's first generation distributed computing framework MapReduce
Investigate why close is required after sqlsession is used in mybatties
ArcGIS js api 4. X submergence analysis and water submergence analysis
Automatically fill in body temperature and win10 task plan
2022 - Data Warehouse - [time dimension table] - year, week and holiday
Rédaction de thèses 19: différences entre les thèses de conférence et les thèses périodiques
中金财富公司怎么样,开户安全吗
Leetcode dynamic planning training camp (1-5 days)
Markdown < a > tag new page open link
After route link navigation, the sub page does not display the navigation style problem
Es keyword sorting error reason = fielddata is disabled on text fields by default Set fielddata = true on keyword in order
【目标跟踪】基于帧差法结合卡尔曼滤波实现行人姿态识别附matlab代码
DNS cloud school | quickly locate DNS resolution exceptions and keep these four DNS status codes in mind