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 ...
随机推荐
- SpringBoot整合Mybatis问题
IDEA对xml文件处理的方式不同 在Eclipse中到dao文件与mapper.xml文件只要在同一级目录下即可 在IDEA中mapper.xml要放在resources目录下 注:resource ...
- 使用百度echarts仿雪球分时图(三)
这章节将完成我们的分时图,并使用真实的数据来进行展示分时图. 一天的交易时间段分为上午的09:30~11:30,下午的13:00~15:00两个时间段,因为分时间段的关系,数据是不连续的,所以会先分为 ...
- .NET 使用事物调用存储过程
using System; using System.Collections.Generic; using System.Configuration; using System.Data; using ...
- asp.net 设计音乐网站
第一步 收集资料 http://www.logoko.com.cn/ --设计logo网站 设计音乐文档 https://wenku.baidu.com/view/3d957617f18583 ...
- 微信小程序开发(七)获取手机网络类型
// succ.wxml <view>手机网络状态:{{netWorkType}}</view> // succ.js var app = getApp() Page({ da ...
- Linux磁盘及文件系统管理4
文件系统的使用: 首先要“挂载”:mount命令和umount命令 根据文件系统之外的其它文件系统要想能够被访问,都必须通过“关联”到根文件系统上的某个目录来实现,此关联操作即为“挂载”:此目录即为“ ...
- 大幅提升Delphi Datasnap数据传输效率的方法
方法一:增加TCP读写缓存的大小 DataSnap Server中负责TCP/IP通讯的组件是TDSTCPServerTransport,它默认的TCP/IP读写缓冲区的大小为32KB,由 ...
- 说说lock到底锁谁(I)?
写在前面 最近一个月一直在弄文件传输组件,其中用到多线程的技术,但有的地方确实需要只能有一个线程来操作,如何才能保证只有一个线程呢?首先想到的就是锁的概念,最近在我们项目组中听的最多的也是锁谁,如何锁 ...
- Xadmin相关实现
一,保存搜索条件(Save search conditions) kept conditions(保存条件)的应用场景比较常见,在我们查询时,可以实现多条件的筛选查询,比如:在淘宝上,选择了其中的一个 ...
- centos openjdk
centos openjdk centos openjdk centos openjdk 切换 java jdk