给定一个字符串,其中包含字符’(’,’)’,’[’,’]’,’{‘,’}’,左括号必须匹配右括号,一对匹配的括号不能单独出现单个左括号或者右括号。如:(()[])有效,[(])无效
空字符串也算是有效的。

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

可关注公众号了解更多的面试技巧

LeetCode_20-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: ...

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

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

随机推荐

  1. (转)Idea使用教程以及各种问题解决

    1.可以先去查看这篇博客: https://www.cnblogs.com/yjd_hycf_space/p/7483921.html 2.如果到配置tomcat时遇到了问题,找不到tomcat se ...

  2. 脚本代码混淆-Python篇-pyminifier(1)

    前言 最近研究了一下脚本语言的混淆方法,比如 python,javascript等.脚本语言属于动态语言,代码大多无法直接编译成二进制机器码,发行脚本基本上相当于暴露源码,这对于一些商业应用是无法接受 ...

  3. FreeSql (十)更新数据

    FreeSql支持丰富的更新数据方法,支持单条或批量更新,在特定的数据库执行还可以返回更新后的记录值. var connstr = "Data Source=127.0.0.1;Port=3 ...

  4. Java线程常见面试题

    v 多线程实现手段: (1).继承Thread类 (2)实现Runable接口 (3)使用线程池 v 线程控制在那个包:java.util.concurrent. (1)提供了线程的运行.(2)线程池 ...

  5. Nginx的架构及工作流程

    NGINX是一个免费的,开源的,高性能的HTTP服务器和反向代理,以及IMAP / POP3代理服务器.NGINX以其高性能,稳定性,丰富的功能集,简单的配置和低资源消耗而闻名,也是为解决C10K问题 ...

  6. 第一次参与国际空间站ISS 的SSTV活动

    先来看看本次 ISS 的 SSTV活动公告 SSTV Event planned for Early August ARISS News Release                         ...

  7. 360大牛 全面解读 PHP面试

    360大牛全面解读PHP面试 第1章 课程介绍 让大家了解基本面试流程和面试的核心要求以及意义是什么并理解PHP面试考点主要以基础为核心,说明PHP面试考察范围. 第2章 PHP基础知识考察点 本章主 ...

  8. 02.Django基础二之URL路由系统

    一 URL配置 Django 1.11版本 URLConf官方文档 URL配置(URLconf)就像Django 所支撑网站的目录.它的本质是URL与要为该URL调用的视图函数之间的映射表.你就是以这 ...

  9. 过渡 - transition

    过渡 - transition 是变形transfrom其中一种效果,定义为一种状态过渡到另一种状态的过程,今天学习到css3动画,特此记录下过渡的使用和一些效果. 实例1: <div clas ...

  10. 让API实现版本管理的实践

    API版本管理的重要性不言而喻,对于API的设计者和使用者而言,版本管理都有着非常重要的意义.下面会从WEB API 版本管理的角度提供几种常见办法: 首先,对于API的设计和实现者而言,需要考虑向后 ...