括号匹配

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.

class Solution {
public:
bool isValid(string s) {
//use stack
stack<char> st;
for(int i = 0;i<s.size();i++)
if(s[i] == ')' || s[i] == ']' || s[i] == '}')
{
if(st.empty())
return false;
else
{
char c = st.top();
st.pop();
if ((c == '(' && s[i] != ')') || (c == '[' && s[i] != ']') || (c == '{' && s[i] != '}'))
return false;
}
}
else
st.push(s[i]);
return st.empty();
}
};

  

leetcode:Valid Parentheses的更多相关文章

  1. LeetCode 020 Valid Parentheses

    题目描述:Valid Parentheses Given a string containing just the characters '(', ')', '{', '}', '[' and ']' ...

  2. LeetCode之“动态规划”:Valid Parentheses && Longest Valid Parentheses

    1. Valid Parentheses 题目链接 题目要求: Given a string containing just the characters '(', ')', '{', '}', '[ ...

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

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

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

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

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

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

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

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

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

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

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

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

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

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

随机推荐

  1. BZOJ1175 : [Balkan2007]The stairways of Saharna

    杨氏图表,维护若干个单调不下降队列. 每次新加入一个数时,先考虑第一个队列: 如果可以放在最后,则放在最后. 否则找到最小的可以替换的替换掉,再将替换的数放入第二个队列,以此类推. 最后$ans_i= ...

  2. 【wikioi】1029 遍历问题

    题目链接:http://www.wikioi.com/problem/1029/ 算法:数学 本题有个2小技巧. 一棵二叉树的前序遍历a1a2a3...ai和后序遍历b1b2b3...bi有一种关系: ...

  3. BJOI2015 Day3

    (wzj这蒟蒻终于滚Cu了,今天第一题SB题写+调用了1.5h,测试时还WA了一个点.第二题数位DP20分钟写完遇到鬼打墙,中间一切正常最后输出一坨负数.调了1h发现是一个数组开小了.又花了20+mi ...

  4. redis运用连接池报错解决

    redis使用连接池报错解决redis使用十几小时就一直报异常 redis.clients.jedis.exceptions.JedisConnectionException: Could not g ...

  5. CentOS GO 语言环境的搭建

    go 语言源码安装依赖 ,gcc ,make glibc库,等,上述工具安装省略, 这个是官方地址:http://www.golang.org/ 另外,其源代码更新采用的是mercurial 工具,安 ...

  6. smarty模板中literal标签的使用

    在使用的时候把js等代码写在模板中就报错,加入literal标签后就正确了 <style> {literal} .tr_color{background-color: #9F88FF} { ...

  7. 安装phpunit

    按照网上的提示,将go-pear.phar和phpunit.phar都放到php.exe所在的目录.但是以管理员身份运行时,输入命令php PEAR/go-pear.phar或php -d phar. ...

  8. WSUS更新服务器

    http://windowsupdate.microsoft.com http://*.windowsupdate.microsoft.com   https://*.windowsupdate.mi ...

  9. sonarqube  安装配置

    http:// www.sonarqube.org MySQL Unsupported mysql version: 5.5. Minimal supported version is 5.6. Th ...

  10. HDU 2159 FATE(二维费用背包)

    FATE Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submi ...