leetcode题解:Valid Parentheses(栈的应用-括号匹配)
题目:
Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the input string is valid.
The brackets must close in the correct order, "()" and "()[]{}" are all valid but "(]" and "([)]" are not.
说明:
1)括号可嵌套,如{[()]}
实现:
class Solution {
public:
bool isValid(string s) {
if(s.empty()) return false;//空字符串,false
int len=strlen(s.c_str());
if(len==) return false;//只有一个字符,false
char pre;
stack<char> char_stack;
for(int i=;i<len;i++)
{
if(isPop(s[i])) //该出栈顶元素
{
if(!char_stack.empty())
{
pre=char_stack.top();//取栈顶元素、并出栈
char_stack.pop();
if(!isChar(pre,s[i]))//是否匹配
return false;
}
else return false;
}
else //入栈
{
char_stack.push(s[i]);
}
}
return char_stack.empty();//栈空true,否则false
}
private:
bool isPop(char t) //判断是否是)} ]符号,是则出栈栈顶元素
{
if(t==')'||t=='}'||t==']')
return true;
else
return false;
}
bool isChar(char t1,char t2)//判断两字符t1,t2是否匹配
{
switch (t1)
{
case '(':
{
if(t2==')') return true;
else return false;
break;
}
case '{':
{
if(t2=='}') return true;
else return false;
break;
}
case '[':
{
if(t2==']') return true;
else return false;
break;
}
default:
return false;
}
}
};
leetcode题解:Valid Parentheses(栈的应用-括号匹配)的更多相关文章
- [LeetCode] Longest Valid Parentheses 最长有效括号
Given a string containing just the characters '(' and ')', find the length of the longest valid (wel ...
- 32. Longest Valid Parentheses(最长括号匹配,hard)
Given a string containing just the characters '(' and ')', find the length of the longest valid (w ...
- leetcode 题解 || Valid Parentheses 问题
problem: Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if ...
- Leetcod--20. Valid Parentheses(极简洁的括号匹配)
Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the inpu ...
- 栈应用之 括号匹配问题(Python 版)
栈应用之 括号匹配问题(Python 版) 检查括号是否闭合 循序扫描被检查正文(一个字符)里的一个个字符 检查中跳过无关字符(所有非括号字符都与当前处理无关) 遇到开括号将其压入栈 遇到闭括号时弹出 ...
- [Leetcode] longest valid parentheses 最长的有效括号
Given a string containing just the characters'('and')', find the length of the longest valid (well-f ...
- [LeetCode] 32. Longest Valid Parentheses 最长有效括号
Given a string containing just the characters '(' and ')', find the length of the longest valid (wel ...
- [LeetCode] 20. Valid Parentheses 验证括号
Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the inpu ...
- [LeetCode] 20. Valid Parentheses 合法括号
Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the inpu ...
- [leetcode]20. Valid Parentheses有效括号序列
Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the inpu ...
随机推荐
- 2016-2017 ACM-ICPC, Egyptian Collegiate Programming Contest (ECPC 16)
A.The game of Osho(sg函数+二项展开) 题意: 一共有G个子游戏,一个子游戏有Bi, Ni两个数字.两名玩家开始玩游戏,每名玩家从N中减去B的任意幂次的数,直到不能操作判定为输.问 ...
- BZOJ4888 [Tjoi2017]异或和 【树状数组】
题目链接 BZOJ4888 题解 要求所有连续异或和,转化为任意两个前缀和相减 要求最后的异或和,转化为求每一位\(1\)的出现次数 所以我们只需要对每一个\(i\)快速求出\(sum[i] - su ...
- BZOJ3668 [Noi2014]起床困难综合症 【贪心】
题目 21 世纪,许多人得了一种奇怪的病:起床困难综合症,其临床表现为:起床难,起床后精神不佳.作为一名青春阳光好少年,atm 一直坚持与起床困难综合症作斗争.通过研究相关文献,他找到了该病的发病原因 ...
- Recompile Squid with SSL Bump
https://docs.diladele.com/administrator_guide_4_0/system_configuration/https_filtering/recompile_squ ...
- server reached pm.max_children setting (5), consider raising it
先查看日志 /data1/server/php-cgi/var/log/php-fpm.log[19-Dec-2012 11:41:13] WARNING: [pool www] server rea ...
- js字符串使用占位符拼接
由于几个老项目中经常用到jquery拼接字符串,各种引号很disgusting 所以写了一个占位符拼接的的方法 String.prototype.signMix= function() { if(ar ...
- Binary Indexted Tree 树状数组入门
感谢http://www.cnblogs.com/xudong-bupt/p/3484080.html 树状数组(BIT)是能够完成下述操作的数据结构: 给定一初始值全为零的数列a1,a2a,a3.. ...
- C#读取JSON字符串
下面这个是一段JSON字符串宏观图 下面我们通过C#读取JSON字符串里的任何一个数值 string jsonString="上面JSON字符串"; //需要引用Newtonsof ...
- springBoot Ribbon 负载均衡
1.依赖引用 <!-- 引入关于 eureka-server的依赖 --> <dependency> <groupId>org.springframework.cl ...
- [BZOJ1295][SCOI2009]最长距离 最短路+枚举
1295: [SCOI2009]最长距离 Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 1683 Solved: 912[Submit][Statu ...