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. jquery制作滚动条到一定位置触发

    $(function(){ var nav=$(".nav"); //得到导航对象 var win=$(window); //得到窗口对象 var sc=$(document);/ ...

  2. Python 小知识点(8)-- __new__

    第一段代码如下: class Foo(object): def __init__(self,name): self.name = name print("Foo __init__" ...

  3. C#中的四舍五入有多坑

    原文:C#中Math.Round()实现中国式四舍五入 C#中的Math.Round()并不是使用的"四舍五入"法.其实在VB.VBScript.C#.J#.T-SQL中Round ...

  4. (一)Spring’s MVC Architecture

    Spring’s MVC module Spring’s MVC module is based on front controller design pattern followed by MVC ...

  5. jar 运行报错:找不到或无法加载主类

    NIFEST.MF文件中指定的,如下所示:Manifest-Version: 1.0Class-Path: .Main-Class: com.webex.app.Main             // ...

  6. 使用jxl 解析Excel

    jxl(Java Excel API) 使用方法 [1]    构建Workbook对象, 只读Workbook对象  //   直接从本地文档创建Workbook  //   从输入流创建Workb ...

  7. ROS解决网页断流现象 (转)

    “pppoe-client”接口,将“MAX MTU”和“MAX MRU”都设置成“” /ip firewall mangle add action=change-mss chain=forward ...

  8. 前端开发之CSS入门篇

    一.CSS介绍和语法 二.CSS引入方式 三.基本选择器 四.高级选择器 五.伪类选择器 六.伪元素选择器 1️⃣  CSS介绍和语法 1. CSS的介绍 (1)为什么需要CSS? 使用css的目的就 ...

  9. Linux实战教学笔记31:Keepalived高可用集群应用实践

    1.1 Keepalived高可用软件 1.1.1 Keepalived介绍 Keepalived软件起初是专门为LVS负载均衡软件设计的,用来管理并监控LVS集群系统中各个服务节点的状态,后来又加入 ...

  10. Perl 变量:哈希变量

    Perl 哈希变量哈希是 key/value 对的集合.Perl中哈希变量以百分号 (%) 标记开始.访问哈希元素格式:${key}. 1.创建哈希创建哈希可以通过以下两种方式: 1.为每个 key ...