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. [转]OpenMP 入门指南

    简介 这门课作为 ECE 中少有的跟计算机科学相关的课,自然是必上不可.不过无论是 OpenMP 还是 CUDA,对于平时极少接触并行编程的我来说,都是十分吃力的,第一次作业的 OpenMP 编程已经 ...

  2. hibernate的多对多关联映射

    在我们实际项目中,多对多的情况也时长存在,比如最常见的就是系统管理的五张表,如下面的一个结构: 在本文学习hibernate多对多关联映射的实验中我简单的写几个字段,达到学习目的即可. 1.多对多的关 ...

  3. Workflow规则收藏

    豆瓣电影  查看电影评分等详细信息 查看图片EXIF 图铃机器人 快递查询 翻译 手机号码归属地 音乐视频下载 获取附近的免费WIFI

  4. css-position属性实例1

    一:position说明 position fixed 实现固定在某个位置 正常情况写两个div是在一层中,通过position属性,可以实现分两层和固定,就像在墙上贴了一层纸,就分了两层了. pos ...

  5. java注解方式解析xml格式

    注解类和字段方式: @XStreamAlias("message") 别名注解 注解集合: @XStreamImplicit(itemFieldName="part&qu ...

  6. Servlet 3.0 新特性详解

    转自:http://www.ibm.com/developerworks/cn/java/j-lo-servlet30/#major3 Servlet 是 Java EE 规范体系的重要组成部分,也是 ...

  7. 字符串匹配的kmp算法 及 python实现

    一:背景 给定一个主串(以 S 代替)和模式串(以 P 代替),要求找出 P 在 S 中出现的位置,此即串的模式匹配问题. Knuth-Morris-Pratt 算法(简称 KMP)是解决这一问题的常 ...

  8. linux网络管理基本命令

    1.last 显示所有用户的登录情况 2.lastlog 显示那些用户有登录过,那些用户从来没登录过 3.traceroute 探测我指定网站的路径,来跟踪路由 .如:traceroute   www ...

  9. getImplementationVersion-获取版本号

    在工作中会遇到这个方法的使用,就记录一下. 一:getImplementationVersion 1.  方法 java.lang.Package.getImplementationVersion() ...

  10. ref:spring配置数据库方式

    ref:https://blog.csdn.net/alsyuan/article/details/73239240 1.使用org.springframework.jdbc.datasource.D ...