LeetCode_20-Valid Parentheses
给定一个字符串,其中包含字符’(’,’)’,’[’,’]’,’{‘,’}’,左括号必须匹配右括号,一对匹配的括号不能单独出现单个左括号或者右括号。如:(()[])有效,[(])无效
空字符串也算是有效的。
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的更多相关文章
- [LeetCode] Longest Valid Parentheses 最长有效括号
Given a string containing just the characters '(' and ')', find the length of the longest valid (wel ...
- [LeetCode] Valid Parentheses 验证括号
Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the inpu ...
- Longest Valid Parentheses
Given a string containing just the characters '(' and ')', find the length of the longest valid (wel ...
- 72. Generate Parentheses && Valid Parentheses
Generate Parentheses Given a string containing just the characters '(', ')', '{', '}', '[' and ']', ...
- leetcode 32. Longest Valid Parentheses
Given a string containing just the characters '(' and ')', find the length of the longest valid (wel ...
- 【leetcode】Longest Valid Parentheses
Longest Valid Parentheses Given a string containing just the characters '(' and ')', find the length ...
- 【leetcode】 Longest Valid Parentheses (hard)★
Given a string containing just the characters '(' and ')', find the length of the longest valid (wel ...
- [LintCode] Valid Parentheses 验证括号
Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the inpu ...
- Longest Valid Parentheses 每每一看到自己的这段没通过的辛酸代码
Longest Valid Parentheses My Submissions Question Solution Total Accepted: 47520 Total Submissions: ...
- [LeetCode] Longest Valid Parentheses 动态规划
Given a string containing just the characters '(' and ')', find the length of the longest valid (wel ...
随机推荐
- MD5字符串加密
MD5字符串加密 Message Digest Algorithm MD5(中文名为消息摘要算法第五版)为计算机安全领域广泛使用的一种散列函数,用以提供消息的完整性保护.该算法的文件号为RFC 132 ...
- IDEA中工程上传到SVN
1.先在IDEA上集成SVN 2.查看SVN仓库:连接SVN ①此时应该先去SVN服务器中新建一个SVN服务: 点击下一步 点击下一步 点击create ②查看SVN仓库 先复制SVN的地址 把复制的 ...
- 关于jstl和web.xml之间的版本问题
jstl的版本1.2对应web.xml3.1版本 jstl的版本1.1对应web.xml2.3版本 (IDEA中默认创建的是2.3的web.xml,最好换成3.1版本的) web.xml模板: < ...
- 1.mssql和mysql sql如何调优?
mssql是指微软的SQL Server数据库服务器. mssql常见的调优方法主要分为两类, (1)代码优化,代码优化,我搜集常用到的有以下几点: 1.避免使用select * 语句 2.避免关联 ...
- 百度地图WEB端判断用户是否在网格范围内
在pc端设置商家的配送范围,用户在下单时,根据用户设置的配送地点判断是否在可配送范围内,并给用户相应的提示. 下面说下我的实现思路: 1.用百度地图在PC端设置配送范围,可拖拽选择 2.根据用户设置的 ...
- Flink入门宝典(详细截图版)
本文基于java构建Flink1.9版本入门程序,需要Maven 3.0.4 和 Java 8 以上版本.需要安装Netcat进行简单调试. 这里简述安装过程,并使用IDEA进行开发一个简单流处理程序 ...
- 4.1、顺序栈的实现(java实现)
1.实现源码 public class SeqStack { private final int MaxSize = 8; private int top; //栈顶 private Object s ...
- apache ignite系列(八):问题汇总
1,java.lang.ClassNotFoundException Unknown pair 1.Please try to turn on isStoreKeepBinary in cache s ...
- ios 把数组对象转成json字符串存起来
1第一步是我们获取数据源 一般我们都是从接口请求数据 NSArray *subColumnsArray = nil; NSDictionary *dict = [NSJSONSerialization ...
- Docker常用命令小记
除了基本的docker pull.docker image.docker ps,还有一些命令及参数也很重要,在此记录下来避免遗忘. 环境信息 以下是本次操作的环境: 操作系统:CentOS Linux ...