leetcode 题解 || Valid Parentheses 问题
problem:
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.
输入一个包括上述六种括号的字符串,检查括号是否能成功匹配
thinking:
(1)这是简化的表达式解析,匹配的方法是:
从低位到高位遍历字符串。出现左側括号('('、’[‘、’{‘)则入栈,出现右側括号('('\、'['、'{')则从栈中取出一个符号与之配对。
当出现:要从栈取字符时而栈为空、字符串遍历完而栈不为空 这 两种情况时,匹配失败。
(2)string s="abcd",最低位是a。别犯低级错误!
!
!!!
code:
class Solution {
protected:
bool check(char a,char b)
{
cout<<"a: "<<a<<"b: "<<b<<endl;
bool flag = false;
if(a=='(')
{
if(b==')')
flag=true;
}
else if(a=='[')
{
if(b==']')
flag=true;
}
else
{
if(b=='}')
flag=true;
}
return flag; }
public:
bool isValid(string s) {
string str=s;
bool result=true;
int length = str.size();
stack<char> mystack;
for(int i=0;i<length;i++)
{
cout<<"i="<<i<<"s[i]="<<str.at(i)<<endl;
if(str.at(i)=='('||str.at(i)=='[' || str.at(i)=='{')
{
cout<<"this"<<endl;
mystack.push(str.at(i));
}
else
{
cout<<" here"<<endl;
if(mystack.empty())
return false;
char tmp = mystack.top();
mystack.pop();
result=check(tmp,str.at(i));
if(!result)
return false;
}//else
}//for
if(mystack.size()!=0)
return false;
else
return true; }
private:
string str;
};
leetcode 题解 || Valid Parentheses 问题的更多相关文章
- [LeetCode] Longest Valid Parentheses 最长有效括号
Given a string containing just the characters '(' and ')', find the length of the longest valid (wel ...
- [LeetCode] Longest Valid Parentheses 解题思路
Given a string containing just the characters '(' and ')', find the length of the longest valid (wel ...
- [Leetcode] longest valid parentheses 最长的有效括号
Given a string containing just the characters'('and')', find the length of the longest valid (well-f ...
- [LeetCode] Longest Valid Parentheses -- 挂动态规划羊头卖stack的狗肉
(Version 1.3) 这题在LeetCode上的标签比较有欺骗性,虽然标签写着有DP,但是实际上根本不需要使用动态规划,相反的,使用动态规划反而会在LeetCode OJ上面超时.这题正确的做法 ...
- [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] Longest Valid Parentheses
第一种方法,用栈实现,最容易想到,也比较容易实现,每次碰到‘)’时update max_len,由于要保存之前的‘(’的index,所以space complexity 是O(n) // 使用栈,时间 ...
- [LeetCode] 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 ...
随机推荐
- C. Ayoub and Lost Array(DP)
(又是被队友带着上分的一场--) 题目链接:http://codeforces.com/contest/1105/problem/C 题目大意:给你n,l,r.每一个数都是在l,r范围之内,然后问你这 ...
- linux笔记_day09
1.运算器.控制器.存储器.输入输出(IO) 地址总线:内存寻址 数据总线:传输数据 控制总线:控制指令 寄存器:cpu暂时存储器 2.系统设定 默认输出设备:标准输出,STDOUT,1(描述符)(显 ...
- 底板芯片组与内存映射(Motherboard Chipsets and the Memory Map) 【转】
转自:http://blog.chinaunix.net/uid-25909619-id-4194650.html 底板芯片组与内存映射 我打算写一些关于计算机内部构造(computer intern ...
- vue 兼容IE报错解决方案
IE 页面空白 报错信息 此时页面一片空白 报错原因 Babel 默认只转换新的 JavaScript 语法(如箭头函数),而不转换新的 API ,比如 Iterator.Generator.Set. ...
- WCF服务安全控制之netTcpBinding的用户名密码验证【转】
选择netTcpBinding WCF的绑定方式比较多,常用的大体有四种: wsHttpBinding basicHttpBinding netTcpBinding wsDualHttpBinding ...
- appium无ID、name定位处理【转】
1.关于没有name,没有ID的元素的定位---通用篇解题思路:因为没有name,id:其实剩下的选择已不多,要么xpath,要么className.xpath木有好印象(稳定性不高,加之1.0x后需 ...
- spring动态加载(刷新)配置文件 [复制链接]
待验证 在程序开发时,通常会经常修改spring的配置文件,不得不重启tomcat来加载spring配,费时费力.如果能在不重启tomcat的情况下,手动动态加载spring 配置文件,动态重启读取s ...
- Python_oldboy_自动化运维之路_socket编程(十)
链接:http://www.cnblogs.com/linhaifeng/articles/6129246.html 1.osi七层 引子: 须知一个完整的计算机系统是由硬件.操作系统.应用软件三者组 ...
- TPCC-MySQL安装、使用及结果解读
tpcc-mysql用于MySQL基准测试,percona基于TPC-C(下面简写成TPCC)衍生出来的产品.下面对tpcc-mysql进行安装然后使用,最后结果解读,安装very easy,let ...
- nodejs mysql 执行多条sql语句
执行多条查询语句 为了安全起见,默认情况下是不允许执行多条查询语句的.要使用多条查询语句的功能,就需要在创建数据库连接的时候打开这一功能: var connection = mysql.createC ...