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. JS知识点概况

    1.什么是JavaScript a)   JavaScript 被设计用来向 HTML 页面添加交互行为. b)   JavaScript 是一种脚本语言(脚本语言是一种轻量级的编程语言). c)   ...

  2. VS2013 编译错误 error: MSB8031

    VS2010 创建的 MFC 程序,用 VS2013 打开后编译出现错误: C:\Program Files (x86)\MSBuild\Microsoft.Cpp\v4.0\V120\Microso ...

  3. Java并发编程之闭锁简介

    闭锁相当于一扇门,在闭锁到达结束状态之前,这扇门一直是关闭着的,没有任何线程可以通过,当到达结束状态时,这扇门才会打开并容许所有线程通过.它可以使一个或多个线程等待一组事件发生.闭锁状态包括一个计数器 ...

  4. Java虚拟机:JVM中的Stack和Heap

    简单的了解一下JVM中的栈和堆 在JVM中,内存分为两个部分,Stack(栈)和Heap(堆),这里,我们从JVM的内存管理原理的角度来认识Stack和Heap,并通过这些原理认清Java中静态方法和 ...

  5. PHP配置xdebug

    其实已经做PHP超过2年了,但是今天特别有感触,所以把过程写在这里 环境是win7+apache2.2+php5.3,因为某种原因,必须使用这个版本. 然后就死活配置不出来.apache日志如下: [ ...

  6. opencv菜鸟学习之旅cvNorm

    Norm 计算数组的绝对范数, 绝对差分范数或者相对差分范数 double cvNorm( const CvArr* arr1, const CvArr* arr2=NULL, int norm_ty ...

  7. File.ReadAllText 读取中文乱码问题

    原文:File.ReadAllText 读取中文乱码问题 用 File.ReadAllText(filepath)   去读取中文txt文件会遇到乱码问题,我自己测试了一下,ANSI编码的txt才会出 ...

  8. Unix/Linux环境C编程入门教程(28) 日期时间那些事儿

    记得这个专题第一篇我们写过一个程序运行时间的程序,采用库函数提供的clock()模拟做程序测试.本篇介绍的函数也是和时间相关,但是没有clock的细致,而是提供的系统时间和日期. 1.asctime( ...

  9. 获取windows身份认证网站页面内容

    有些网站必须登录才能获取到页面内容. 代码如下,可获取数据. var url = "https://yunda-api-test.appspot.com/int/parcel?wait=tr ...

  10. LInux 下挂在Windows共享文件夹

    挂载WIndow共享文件夹  //192.168.0.103/software mount -t smbfs -o username=administrator,password=“de123”  / ...