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 ...
随机推荐
- glibc内存泄露以及TCmalloc 简单分析
最近开发一个私人程序时碰到了严重的内存问题,具体表现为:进程占用的内存会随着访问高峰不断上升,直到发生OOM被kill为止.我们使用valgrind等工具进行检查发现程序并无内存泄露,经过仔细调查我们 ...
- 2017 多校4 Dirt Ratio
多校4 Dirt Ratio(二分+线段树) 题意: 给出n个数,找一段区间使得区间内不同数字个数除以区间长度最小,求这个最小值,\(n<=60000,a_i<=n\) 题解: 二分答案m ...
- BZOJ1017 [JSOI2008]魔兽地图DotR 【树形dp + 背包dp】
题目链接 BZOJ1017 题解 orz hzwer 树形dp神题 设\(f[i][j][k]\)表示\(i\)号物品恰好花费\(k\)金币,并将\(j\)个物品贡献给父亲的合成时的最大收益 计算\( ...
- 如何用js让表格的行也能拖动
行拖动的实现思路非常简单,选中一行,往上拖就与上面的行交换位置,往下拖就与下面的行交换位置.问题是如何得到交换行.我见过一个非常详细的教程,它会把表格里的每一行的高度与Y坐标计算出来,换言之,都时是比 ...
- nginx简易部署
Nginx (engine x) 可作为web反向代理服务器.能够代理外部网络上的主机,访问内部网络 1 首先windows下载nginx :http://nginx.org/download/ngi ...
- 【 D3.js 进阶系列 — 4.0 】 绘制箭头
转自:http://www.ourd3js.com/wordpress/?p=660 [ D3.js 进阶系列 — 4.0 ] 绘制箭头 发表于2014/12/08 在 SVG 绘制区域中作图,在绘制 ...
- rest项目的基础返回类设计
package com.hmy.erp.api.vo; import java.io.Serializable; import lombok.Data; /** * erp基本状态返回类 * * @a ...
- MVC学习__修改工程端口号
有时候,我们会希望修改工程默认生成的端口号,方法如下:
- 9.OpenStack安装web界面
安装仪表板 安装仪表板组件 yum install -y openstack-dashboard httpd mod_wsgi memcached python-memcached 编辑/etc/op ...
- Centos 多线程下载工具-axel
32位CentOS执行下面命令: wget -c http://pkgs.repoforge.org/axel/axel-2.4-1.el5.rf.i386.rpm rpm -ivh axel-2.4 ...