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. leetcode442

    public class Solution { Dictionary<int, int> dic = new Dictionary<int, int>(); public IL ...

  2. linux系统构架 - LB集群之LVS的NAT

    1.环境说明 三台服务器,一台叫dir,两台叫rs1和rs2 (director 和 real server) dir外网ip:192.168.192.129  内网ip:192.168.1.114 ...

  3. 收集了一些iOS技术面试题

    1.Difference between shallow copy and deep copy?
浅复制和深复制的区别? 
答案:浅层复制:只复制指向对象的指针,而不复制引用对象本身.
深层复制:复制 ...

  4. cdh 安装步骤

    http://www.cnblogs.com/jasondan/p/4011153.html 关于CDH和Cloudera Manager CDH (Cloudera's Distribution, ...

  5. java核心知识点 --- 线程池ThreadPool

    线程池是多线程学习中需要重点掌握的. 系统启动一个新线程的成本是比较高的,因为它涉及与操作系统交互.在这种情形下,使用线程池可以很好的提高性能,尤其是当程序中需要创建大量生存期很短暂的线程时,更应该考 ...

  6. Nginx负载均衡高可用

    1.   Nginx负载均衡高可用 首先介绍一下Keepalived,它是一个高性能的服务器高可用或热备解决方案,Keepalived主要来防止服务器单点故障的发生问题,可以通过其与Nginx的配合实 ...

  7. glTexGen

    [glTexGen] Rather than having to explicitly provide a texture coordinate for each vertex, we can use ...

  8. nginx源码完全注释(1)ngx_alloc.h / ngx_alloc.c

    首先看 ngx_alloc.h 文件,主要声明或宏定义了 ngx_alloc,ngx_calloc,ngx_memalign,ngx_free. /* * Copyright (C) Igor Sys ...

  9. Computer2

    luo@luo-All-Series:~/MyFile/TensorflowProject$ conda create -n flappbird1 python=3.7Solving environm ...

  10. ubuntu16.04 安装opencv3

    (opencvC++) luo@luo-ThinkPad-W540:20181205$ conda install --channel https://conda.anaconda.org/menpo ...