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. Expression Tree Build

    The structure of Expression Tree is a binary tree to evaluate certain expressions.All leaves of the ...

  2. Number of Airplanes in the Sky

    Given an interval list which are flying and landing time of the flight. How many airplanes are on th ...

  3. Linux 内核中断内幕【转】

    转自:http://www.ibm.com/developerworks/cn/linux/l-cn-linuxkernelint/ 本文对中断系统进行了全面的分析与探讨,主要包括中断控制器.中断分类 ...

  4. 破解验证码模拟登陆cnblogs

    from selenium import webdriver from selenium.webdriver import ActionChains from PIL import Image imp ...

  5. django之class Meta

    通过一个内嵌类 "class Meta" 给你的 model 定义元数据, 类似下面这样: class Foo(models.Model): bar = models.CharFi ...

  6. C#解除文件锁定

    public static void StreamsFile(string fi) { try { var p = new Process { StartInfo = { FileName = Env ...

  7. Java 日期时间获取和显示

    Java正确获取星期Calendar.DAY_OF_WEEKhttp://chamcon.iteye.com/blog/2144433 Java SimpleDateFormat 中英文时间格式化转换 ...

  8. 关于.NetCore 2.0 迁移到2.1的一些建议和问题

    最近手欠升级了下VS2017到15.7版本 然后更新了下sdk到2.1.300版本 那么麻烦就来了,原有项目就带来了很多问题,更新所有包到最新就不用说了 下面说明下最主要存在的2问题: 第一个问题:调 ...

  9. SSIS组件——Merge、Merge Join、Union All

  10. java中的二进制运算简单理解

    package test9; public class StreamTest { public static void main(String[] args) { int a = 15;// 0b11 ...