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.

题解: 典型的STL stack 应用。注意边界条件。

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

转载请注明出处: http://www.cnblogs.com/double-win/ 谢谢!

[LeetCode 题解]: Valid Parentheses的更多相关文章

  1. leetcode 题解 || Valid Parentheses 问题

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

  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 (wel ...

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

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

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

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

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

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

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

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

  8. [LeetCode] Longest Valid Parentheses

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

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

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

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

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

随机推荐

  1. RTTI(一) 枚举

    SetEnumProp void __fastcall TForm2::Button1Click(TObject *Sender) { //Getting the current color of t ...

  2. 关于Web项目出现懒加载异常的解决方案

    manytomany关系中,使用 fetch = FetchType.LAZY 来做懒加载,加快些性能.但是却一直出错,原因是session被关闭,要保持session,需要事务. Hibernate ...

  3. Python nonlocal 与 global 关键字解析

    nonlocal 首先,要明确 nonlocal 关键字是定义在闭包里面的.请看以下代码: x = 0 def outer(): x = 1 def inner(): x = 2 print(&quo ...

  4. 用django框架开发一个B2C购物网站的基本流程和用到的知识点总结1

    开发流程 开发模式采用前后端分离模式,作为后端开发人员我们只关注后端业务逻辑开发: 省略项目框架搭建文件的配置部分.... 一:用户部分 在项目开发中我们要用到用户模型类User,Django认证系统 ...

  5. 网络编程基础之Socket套接字简单应用

    一.Socket套接字实现通信循环 所谓通信循环,简单理解就是客户端可以给服务端循环发送信息并获得反馈的过程. 1.基础版 通信循环的程序分为两部分,即两个python模块,分别为客户端.py和服务端 ...

  6. 简述 ascii、unicode、utf-8、gbk 的关系 (全网最全!!!)

    ascii 是最早美国用的标准信息交换码,把所有的字母的大小写,各种符号用 二进制来表示,共有256中,加入些拉丁文等字符,1bytes代表一个字符, Unicode是为了统一世界各国语言的不用,统一 ...

  7. Gym101128G:Game of Cards

    题意: 有P摞纸牌和一个数字k,每次可以从一摞中拿0-k张牌,拿完再剩下的牌中的第一张数字是几,就必须再拿几张,谁不能拿谁输. emmm感觉好像就是裸的SG游戏啊,数据不大,递推出每一摞牌的SG值,然 ...

  8. iOS 导航栏黑线,UIImage 枚举处理方式

      ios 找出导航栏下面的黑线(可隐藏,改变样式等) http://www.jianshu.com/p/effa4a48f1e3     设置UIImage的渲染模式:UIImage.renderi ...

  9. spring aop博客记录

    1.spring aop和事务失效 解决办法: http://blog.csdn.net/z2007130205/article/details/41284381 http://blog.csdn.n ...

  10. phpmailer配置163邮箱

    function send_email($email = ''){ $this->autoRender = false; date_default_timezone_set('PRC'); re ...