当前位置:网站首页>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^4
s
仅由括号'()[]{}'
组成
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
边栏推荐
- Click an EL checkbox to select all questions
- The second method of file upload in form form is implemented by fileitem class, servletfileupload class and diskfileitemfactory class.
- Sqoop imports data from Mysql to HDFS using lzop compression format and reports NullPointerException
- How to do product innovation—— Exploration of product innovation methodology I
- star
- Scripy tutorial - (2) write a simple crawler
- Latest investigation and progress of building intelligence based on sati
- DNS cloud school | quickly locate DNS resolution exceptions and keep these four DNS status codes in mind
- SIGIR'22 "Microsoft" CTR estimation: using context information to promote feature representation learning
- Operation of numpy array
猜你喜欢
How to protect ECs from hacker attacks?
Fundamentals of network communication (LAN, Wan, IP address, port number, protocol, encapsulation and distribution)
Installation and use of NVM
Some basic configurations in interlij idea
An error is reported when sqoop imports data from Mysql to HDFS: sqlexception in nextkeyvalue
Three. Based on ply format point cloud voxel model JS upload interface writing
Es keyword sorting error reason = fielddata is disabled on text fields by default Set fielddata = true on keyword in order
Numpy mathematical function & logical function
Mathematical modeling column | Part 5: MATLAB optimization model solving method (Part I): Standard Model
Livego + ffmpeg + RTMP + flvjs to realize live video
随机推荐
2022dasctf APR x fat epidemic prevention challenge crypto easy_ real
selenium.common.exceptions.WebDriverException: Message: ‘chromedriver‘ executable needs to be in PAT
Actual measurement of automatic ticket grabbing script of barley network based on selenium (the first part of the new year)
bounding box iou
ABAQUS script email auto notification
Scripy tutorial - (2) write a simple crawler
Redis installation (centos7 command line installation)
[graph theory brush question-4] force deduction 778 Swimming in a rising pool
論文寫作 19: 會議論文與期刊論文的區別
I JS deep copy and shallow copy
PCA based geometric feature calculation of PCL point cloud processing (52)
SQL Server Connectors By Thread Pool | DTSQLServerTP 插件使用说明
Installation and use of NVM
Still using listview? Use animatedlist to make list elements move
波场DAO新物种下场,USDD如何破局稳定币市场?
Identification of bolt points in aerial photography based on perception
ArcGIS JS version military landmark drawing (dovetail arrow, pincer arrow, assembly area) fan and other custom graphics
Es error: request contains unrecognized parameter [ignore_throttled]
Confusion about thread blocking after calling the read () method of wrapper flow
中金财富公司怎么样,开户安全吗