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 问题的更多相关文章

  1. [LeetCode] Longest Valid Parentheses 最长有效括号

    Given a string containing just the characters '(' and ')', find the length of the longest valid (wel ...

  2. [LeetCode] Longest Valid Parentheses 解题思路

    Given a string containing just the characters '(' and ')', find the length of the longest valid (wel ...

  3. [Leetcode] longest valid parentheses 最长的有效括号

    Given a string containing just the characters'('and')', find the length of the longest valid (well-f ...

  4. [LeetCode] Longest Valid Parentheses -- 挂动态规划羊头卖stack的狗肉

    (Version 1.3) 这题在LeetCode上的标签比较有欺骗性,虽然标签写着有DP,但是实际上根本不需要使用动态规划,相反的,使用动态规划反而会在LeetCode OJ上面超时.这题正确的做法 ...

  5. [LeetCode] 20. Valid Parentheses 验证括号

    Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the inpu ...

  6. [LeetCode] 20. Valid Parentheses 合法括号

    Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the inpu ...

  7. [LeetCode] Longest Valid Parentheses

    第一种方法,用栈实现,最容易想到,也比较容易实现,每次碰到‘)’时update max_len,由于要保存之前的‘(’的index,所以space complexity 是O(n) // 使用栈,时间 ...

  8. [LeetCode] Longest Valid Parentheses 动态规划

    Given a string containing just the characters '(' and ')', find the length of the longest valid (wel ...

  9. [leetcode]20. Valid Parentheses有效括号序列

    Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the inpu ...

随机推荐

  1. Javascript - 操作符

    操作符(Operator) void 如果void后是数字,就返回NAN,否则返回Undefined. alert(void "hello");//跟的字符 print undef ...

  2. innobackupex不停库的数据备份并恢复到别的服务器上【转】

    1.innobackupex原理: 备份原理 1).首先会开启一个后台检测进程,实时检测myql redo的变化,一旦发现redo中有新日志写入,立即将日志记入后台日志文件xtrabackup_log ...

  3. Linux 黑白界面显示

    2014年1月14日 15:47:47 不知道别人怎么看,反正我觉得黑白配显示很方便阅读 命令: ls 脚本: ~/.bashrc 指令: alias ls='ls --color=never' 命令 ...

  4. conda管理python包

    参考:http://blog.sina.com.cn/s/blog_c3c116470102wlv5.html 查看python,numpy,scipy,matplotlib的版本及安装位置: htt ...

  5. MySQL 5.6表空间传输

    在MySQL 5.6 Oracle引入了一个可移动表空间的特征(复制的表空间到另一个服务器)和Percona Server采用部分备份,这意味着你现在可以备份单个数据库或表:由于Percona Ser ...

  6. VC++一些开发心得与调试技巧

         1.如何在Release状态下进行调试 Project->Setting=>ProjectSetting对话框,选择Release状态.C/C++标签中的Category选Gen ...

  7. SqlServer 中查询子节对应的上级自定义函数

    CREATE FUNCTION [dbo].[FN_TopGetOrgByUserName] ( @UserName NVARCHAR(128) ) RETURNS @showOrg TABLE(id ...

  8. KnockoutJs学习笔记(九)

    由于component binding部分的内容更为复杂一些,所以这部分我暂时跳过,先学习click binding部分. click binding不仅可以作用于button.input.a等元素, ...

  9. .NetCore 中如何实现分页以及编写一个URL分页

    首先看下效果 这个分页控件不是很完美,体现下思路就行了,有兴趣的可以自己完善,我把代码贴出来,在这边文章中已有一些介绍 代码 public class UosoPagerTagHelper : Tag ...

  10. jQuery项目赋予Router

    给你的jQuery项目赋予Router技能吧   现在你不会React/Vue都不好意思说自己是前端,不过我相信很多前端项目还是基于jquery类库的传统模式的,假如你有追求的态度使用过require ...