LeetCode 20. 有效的括号(Valid Parentheses )
题目描述
给定一个只包括 '(',')','{','}','[',']' 的字符串,判断字符串是否有效。
有效字符串需满足:
- 左括号必须用相同类型的右括号闭合。
- 左括号必须以正确的顺序闭合。
注意空字符串可被认为是有效字符串。
示例 1:
输入: "()"
输出: true
示例 2:
输入: "()[]{}"
输出: true
示例 3:
输入: "(]"
输出: false
示例 4:
输入: "([)]"
输出: false
示例 5:
输入: "{[]}"
输出: true
解题思路
利用栈的思想,对于左括号直接入栈,对于右括号,判断栈顶元素是否为对应的左括号,若不是则返回false,是则出栈继续遍历下一个括号
代码
class Solution {
public:
bool isValid(string s) {
stack<char> st;
for(int i = ; i < s.length(); i++){
switch(s[i]){
case '(':
case '[':
case '{':
st.push(s[i]);
break;
case ')':{
if(st.empty() || st.top() != '(') return false;
st.pop();
break;
}
case ']':{
if(st.empty() || st.top() != '[') return false;
st.pop();
break;
}
case '}':{
if(st.empty() || st.top() != '{') return false;
st.pop();
break;
}
}
}
if(st.size()) return false;
else return true;
}
};
LeetCode 20. 有效的括号(Valid Parentheses )的更多相关文章
- LeetCode 20. 有效的括号(Valid Parentheses)
20. 有效的括号 20. Valid Parentheses 题目描述 给定一个只包括 '(',')','{','}','[',']' 的字符串,判断字符串是否有效. 有效字符串需满足: 左括号必须 ...
- LeetCode 笔记系列八 Longest Valid Parentheses [lich你又想多了]
题目:Given a string containing just the characters '(' and ')', find the length of the longest valid ( ...
- leetcode第31题--Longest Valid Parentheses
Given a string containing just the characters '(' and ')', find the length of the longest valid (wel ...
- leetcode解题报告 32. Longest Valid Parentheses 用stack的解法
第一道被我AC的hard题!菜鸡难免激动一下,不要鄙视.. Given a string containing just the characters '(' and ')', find the le ...
- LeetCode (32) Longest Valid Parentheses
题目 Given a string containing just the characters '(' and ')', find the length of the longest valid ( ...
- Java实现 LeetCode 20 有效的括号
20. 有效的括号 给定一个只包括 '(',')','{','}','[',']' 的字符串,判断字符串是否有效. 有效字符串需满足: 左括号必须用相同类型的右括号闭合. 左括号必须以正确的顺序闭合. ...
- LeetCode 20:有效的括号 Valid Parentheses
给定一个只包括 '(',')','{','}','[',']' 的字符串,判断字符串是否有效. Given a string containing just the characters '(', ' ...
- LeetCode第[20]题(Java):Valid Parentheses
题目:有效的括号序列 难度:Easy 题目内容: Given a string containing just the characters '(', ')', '{', '}', '[' and ' ...
- [Swift]LeetCode20. 有效的括号 | Valid Parentheses
Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the inpu ...
随机推荐
- SIP 3pcc
3PCC全称Third Party Call Control,中文即第三方电话呼叫控制,指的是由第三方控制者在另外两者之间建立一个会话,由控制者负责会话双方的媒体协商.3PCC是一种非常灵活的会话控制 ...
- 如何部署struts开发环境
1 首先登陆http://archive.apache.org/dist/struts/source/页面,会看到struts的下载页面 2 下载struts的最新版本struts2-2.2.1-sr ...
- 《浏览器工作原理与实践》<04>从输入URL到页面展示,这中间发生了什么?
“在浏览器里,从输入 URL 到页面展示,这中间发生了什么? ”这是一道经典的面试题,能比较全面地考察应聘者知识的掌握程度,其中涉及到了网络.操作系统.Web 等一系列的知识. 在面试应聘者时也必问这 ...
- (十一)tina | openwrt关闭调试串口(DEBUG UART)
//编辑以下文件 vi target/allwinner/astar-parrot/base-files/etc/inittab //不同系统文件路径注意更改 //文件内容如下,注释::askcon ...
- CAFFE(四):Ubuntu 下安装jupyter notebook
第一步.安装 pycaffe notebook 接口环境 在上一步成功安装 caffe 之后,就可以通过 caffe 去做训练数据集或者预测各种相关的事了,只不过需要在命令行下通过 caffe 命令进 ...
- OpenJudge POJ C19C 贪心
https://cn.vjudge.net/contest/309482#problem/C #include<bits/stdc++.h> using namespace std; ty ...
- jquery.table2excel,将HTML的table标签数据导出成excel
<!DOCTYPE> <html> <head> <meta http-equiv="Content-Type" content=&quo ...
- kudu_cm_web安装
[root@Node2 opt]# echo never > /sys/kernel/mm/transparent_hugepage/defrag[root@Node2 opt]# echo n ...
- hbuilderX创建vue项目之添加router路由(前端萌新)
作为一个刚刚接触前端不久的新人来说,熟悉了一种目录结构或者项目创建方法以后,恨不得一辈子不会变! 可是人要生活,就要工作,既然是打工,当然要满足雇佣者的要求. 今天我来说说 hbuilderX 这个开 ...
- qt触摸屏隐藏鼠标指针
方法1:运行加参数-nomouse 方法2:QWidget::setCursor(QCursor(Qt::BlankCursor) 例:this->setCursor(Qt::BlankCurr ...