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) {
stack<char> stk;
int size = s.size();
for(int i=;i<size;i++){
if(s[i]=='('||s[i]=='['||s[i]=='{'){
stk.push(s[i]);
}else{
if(stk.empty()){
return false;
}
char topCh = stk.top();
if((s[i]==']' && topCh=='[' )||
(s[i]==')' && topCh=='(' )||
(s[i]=='}' && topCh=='{' )
){
stk.pop();
}else{
return false;
}
}
}
return stk.empty()? true:false;
}
};

[string]Valid Parentheses的更多相关文章

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

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

  2. [LeetCode] Valid Parentheses 验证括号

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

  3. Longest Valid Parentheses

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

  4. 72. Generate Parentheses && Valid Parentheses

    Generate Parentheses Given a string containing just the characters '(', ')', '{', '}', '[' and ']', ...

  5. leetcode 32. Longest Valid Parentheses

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

  6. 【leetcode】Longest Valid Parentheses

    Longest Valid Parentheses Given a string containing just the characters '(' and ')', find the length ...

  7. 【leetcode】 Longest Valid Parentheses (hard)★

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

  8. [LintCode] Valid Parentheses 验证括号

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

  9. Longest Valid Parentheses 每每一看到自己的这段没通过的辛酸代码

    Longest Valid Parentheses My Submissions Question Solution  Total Accepted: 47520 Total Submissions: ...

随机推荐

  1. 论i++与++i

    网上看到好多人问i++与++i到底怎么理解,网友给出的答案几乎都是一样的.如下: i++:先进行计算,然后i自增1 ++i:i自增1,然后进行计算 并且课本上给出的解释跟这个也差不多,不过这样记起来既 ...

  2. 如何查看SQLServer数据库每个表占用的空间大小?

    如何查看SQLServer数据库每个表占用的空间大小? 创建存储过程: CREATE PROCEDURE [dbo].[sys_viewTableSpace]AS BEGIN SET NOCOUNT ...

  3. GIS-开发例程、源代码、MapXtreme、Map (转)

    [原创]MapXtreme实用技巧与源码10例 普通图片生成MapInfo格式电子地图的步骤  http://blog.csdn.net/hmbb2008/category/184134.aspx 基 ...

  4. H-Index,H-Index II

    1.H-Index Total Accepted: 19058 Total Submissions: 70245 Difficulty: Medium Given an array of citati ...

  5. C# 与MySQL

    1. MySQL.Data.dll       http://files.cnblogs.com/files/lwngreat/MySql.Data.rar 2.在工程中添加引用 3. 使用  Mys ...

  6. java poi 导出excel

    poi的jar下载地址:http://poi.apache.org/ 下载后会有很多jar,但是如果只是简单的excel报表的话,导入一个poi-版本号-日期.jar就可以了. 导出代码: priva ...

  7. svg 文字

    <text>标签 在svg中用使用<text>标签去定义一段文字.如 Example 1 在svg中写下 在平坦的路上曲折前行 Example 1 Dome <svg h ...

  8. twisted 使用

    工欲善其事,必先利其器,我们先来进行 twisted 框架的安装,由于平时使用的都是 Windows 系统,那么下面我们就讲解下 Windows 下 twisted 框架的安装(1)下载 twiste ...

  9. lda模型的python实现

    LDA(Latent Dirichlet Allocation)是一种文档主题生成模型,最近看了点资料,准备使用python实现一下.至于数学模型相关知识,某度一大堆,这里也给出之前参考过的一个挺详细 ...

  10. ACboy needs your help again!--hdu1702

    ACboy needs your help again! Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K ( ...