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

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. C#客户端程序Visual Stadio远程调试

    一,需求来源 在开发过程中,可能会要使用Win7 ,Win8 ,Win10等不同版本的系统去做兼容性调试,也有时候会去针对特别的显卡,无线网卡等等硬件设备的机器做优化,有一种较优的方案,那就是使用Vi ...

  2. 运行sudo apt-get install nginx时报错有几个软件包无法下载,要不运行 apt-get update 或者加上 --fix-missing 的选项再试试?解决

    运行sudo apt-get install nginx时报错有几个软件包无法下载,要不运行 apt-get update 或者加上 --fix-missing 的选项再试试?解决办法 第一步:运行s ...

  3. Storm 系列(六)—— Storm 项目三种打包方式对比分析

    一.简介 在将 Storm Topology 提交到服务器集群运行时,需要先将项目进行打包.本文主要对比分析各种打包方式,并将打包过程中需要注意的事项进行说明.主要打包方式有以下三种: 第一种:不加任 ...

  4. 获得本机IP和访问服务的端口号(Java)

    1. //获取本机ip地址 InetAddress addr = InetAddress.getLocalHost(); String ip=addr.getHostAddress().toStrin ...

  5. 【原创】我们还需要学jQuery吗?

    引言 最近撸Vue的项目,感觉的有点心累.恰巧近日,有读者来信,就是想咨询一下 烟哥,现在还有必要学习jQuery么? 我明白,现在MVVM框架逐渐占据了主要市场,很多老项目也逐渐的从jQuery转向 ...

  6. php异或计算绕过preg_match()

    原理以制作免杀马为例:     在制作免杀马的过程,根据php的语言特性对字符进行!运算会将字符类型转为bool类型,而bool类型遇到运算符号时,true会自动转为数字1,false会自动转为数字0 ...

  7. 加入百度地图遇到 framework not found BaiduMapAPI***

    新建一个文件夹,把需要的静态库和.h文件放到文件夹里面.就解决啦.虽然不知道为什么这样可以,但是可以使用啦~~~

  8. 用vetr.x写一个HTTP接口适配器, 对接各种形式接口

    用vetr.x写一个HTTP接口适配器, 对接各种形式接口 项目地址:https://github.com/hjx601496320/transmit 业务说明 在日常开发工作中,我们经常会遇到要和各 ...

  9. Linux 笔记 - 第十八章 Linux 集群之(三)Keepalived+LVS 高可用负载均衡集群

    一.前言 前两节分别介绍了 Linux 的高可用集群和负载均衡集群,也可以将这两者相结合,即 Keepalived+LVS 组成的高可用负载均衡集群,Keepalived 加入到 LVS 中的原因有以 ...

  10. Day3 目录结构及文件管理

    Windows:以多根的方式组织文件C :  D:   E:   F: linux:以单根的方式组织文件 / 2.存放命令相关的目录 /bin 普通用户的使用的命令 /bin /ls ,/bin/da ...