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 ...
随机推荐
- P4397 [JLOI2014]聪明的燕姿
P4397 [JLOI2014]聪明的燕姿 题目背景 阴天傍晚车窗外 未来有一个人在等待 向左向右向前看 爱要拐几个弯才来 我遇见谁会有怎样的对白 我等的人他在多远的未来 我听见风来自地铁和人海 我排 ...
- FOJ ——Problem 1759 Super A^B mod C
Problem 1759 Super A^B mod C Accept: 1368 Submit: 4639Time Limit: 1000 mSec Memory Limit : 32 ...
- Linux下kill命令
首先了解什么是信号:信号是进程级的中断请求,系统定义了30余种信号,kill是管理员用来发送信号的一种手段. 功能说明:删除执行中的程序或工作. 语 法:kill [-s <信息名称或编号> ...
- POJ 2676 数独+dfs深搜
数独 #include "cstdio" #include "cstring" #include "cstdlib" #include &q ...
- Runtime.getRuntime().exec方法
Runtime.getRuntime().exec()方法主要用于执行外部的程序或命令. Runtime.getRuntime().exec共有六个重载方法: public Process exec( ...
- JSP中的:request.getScheme()+"://"+request.getServerName()+":"+request.getServer
String path = request.getContextPath(); String basePath = request.getScheme()+"://"+reque ...
- MAC删除目录下的“.svn”文件的方法
http://bbs.feng.com/read-htm-tid-7803070.html MAC删除目录的“.svn”文件:打开终端,进到所在的目录,然后出入一下代码find . -name &qu ...
- Curl请求方法封装
/** * GET请求 * @param $url * @return bool|mixed */ function http_get($url) { $oCurl = curl_init (); i ...
- 利用NPOI组件产Excel完整操作
最终还是要使用NPOi了.刚开始做的是用com组件,发现如果本机不按照excel就不能使用,后来把其中一支改为了用Itextsharp产生pdf,但是还有几支批次要产生Excel,只能改用NPOI了. ...
- Js 利用正则表达式和replace函数获取string中所有被匹配到的文本
js的replace函数除了替换文本以外还有获取所有被正则表达式匹配到的文本的功能.这里以一个简单的案例来作为演示. 利用正则查找出所有被两个花括号包裹的字符串: var str = '<div ...