括号匹配

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. 【BZOJ】1600: [Usaco2008 Oct]建造栅栏(dp)

    http://www.lydsy.com/JudgeOnline/problem.php?id=1600 说好的今天开始刷水.. 本题一开始我以为是排列组合,但是自己弱想不出来,只想到了如果四边有一条 ...

  2. 【wikioi】1108 方块游戏(模拟)

    http://wikioi.com/problem/1108/ 这题有点变态,因为他根本没有策略! 还是说这题不是实时的?反正这题很变态,是在一个时间段同时消除所有的行列斜边,同一时间!!!!!! 所 ...

  3. 【wikioi】1922 骑士共存问题(网络流/二分图匹配)

    用匈牙利tle啊喂?和网络流不都是n^3的吗(匈牙利O(nm), isap O(n^2m) 但是isap实际复杂度很优的(二分图匹配中,dinic是O(sqrt(V)*E),不知道isap是不是一样. ...

  4. Qt SizePolicy 属性

    控件的sizePolicy说明控件在布局管理中的缩放方式.Qt提供的控件都有一个合理的缺省sizePolicy,但是这个缺省值有时不能适合 所有的布局,开发人员经常需要改变窗体上的某些控件的sizeP ...

  5. thinkphp模板中foreach循环没数据的错误解决

    从控制器方法中$this->assign();函数将值传递给html模板 但是模板不显示数据,直接出来的是代码,效果就和html中写了php代码不能解析一样. 原来是我将thinkphp框架的引 ...

  6. C# 从数据库中删除,插入,修改 索引选中条目

    一.删除 1. ) { var currentSelectIndex = usrListView.SelectedIndex; var item = usrView[currentSelectInde ...

  7. PHP对于Session漏洞的防范

    目前,基于PHP的网站开发已经成为目前网站开发的主流,本文笔者重点从PHP网站攻击与安全防范方面进行探究,旨在减少网站漏洞,希望对大家有所帮助! 一.常见PHP网站安全漏洞 对于PHP的漏洞,目前常见 ...

  8. ZZULIOJ 1726 迷宫(BFS+小坑)

    1726: 迷宫 Time Limit: 1 Sec  Memory Limit: 128 MB Submit: 394  Solved: 64 SubmitStatusWeb Board Descr ...

  9. Web 在线文件管理器学习笔记与总结(10)查看文件夹中的内容

    ① 读取文件夹大小 a. 封装计算文件夹大小的函数 b.  打开文件夹 c. 循环判断文件夹下的内容是文件还是文件夹,如果是文件,则累积相加文件的大小:如果是文件夹,则递归调用该函数 注意两个问题: ...

  10. Nginx/LVS/HAProxy负载均衡软件的优缺点详解

    PS:Nginx/LVS/HAProxy是目前使用最广泛的三种负载均衡软件,本人都在多个项目中实施过,参考了一些资料,结合自己的一些使用经验,总结一下. 一般对负载均衡的使用是随着网站规模的提升根据不 ...