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 ...
随机推荐
- (六十五)c#Winform自定义控件-思维导图/组织架构图(工业)
前提 入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章. GitHub:https://github.com/kwwwvagaa/NetWinformControl 码云:ht ...
- Windows下升级Zabbix Agent
这段时间因工作上不太忙,就着手升级下zabbix,从3升级到最新版4.2,服务器端升级还挺快,就是客户端比较耗时了,往往就是看的越简单的东西越耗时间啊. Windows版本的zabbix agent下 ...
- 联想thinkpad如何关闭触摸板
Tinkpad系列很多关闭触摸屏的功能的方法都是没有的!!!比如说1.Fn+F6,或者Fn+某个按键...直接关闭没用比如说2.控制面板,鼠标/键盘,找到触摸开关...间接关闭没用比如说3.我的电脑, ...
- 面试官:服务器安装 JDK 还是 JRE?可以只安装 JRE 吗?
前些日子有知友面试时被问到如题所示的问题,由于他之前没有准备到这些最最基础的知识,没有考虑过这个问题,所以被问到时竟一脸萌币,回答的不是很好.这道题主要考的是对 Java 基础知识的了解,有些同学可能 ...
- hive内部表与外部表区别详细介绍
问题导读:1.创建内部表与外部表的区别是什么?2.external关键字的作用是什么?3.外部表与内部表的区别是什么?4.删除表的时候,内部表与外部表有什么区别?5.load data local i ...
- Java抽象类构造方法
java中抽象类的子类的构造方法会隐含父类的无参构造方法. package com.zempty.abstractclass; public class AbstractDemo01 { public ...
- 12 (OC)* AFNetworking
AFNetworking主要是对NSURLSession和NSURLConnection(iOS9.0废弃)的封装,其中主要有以下类:1). AFHTTPRequestOperationManager ...
- [STL] Implement "vector", ”deque“ and "list"
vector “可增的”数组 vector是一块连续分配的内存,从数据安排的角度来讲,和数组极其相似. 不同的地方就是: (1) 数组是静态分配空间,一旦分配了空间的大小,就不可再改变了: (2) v ...
- 解决mysql不能在查询A表的同时,更新A表的问题
方法: 运用中间表 UPDATE 表名 SET 字段名 = '' WHERE id in (SELECT a.id FROM (SELECT id FROM 表名 WHERE ISNULL(字段名)) ...
- SpringBoot起飞系列-配置文件(三)
一.SpringBoot中的配置文件 说起到配置文件,大家并不陌生,早在springboot之前,我们用ssh,ssm框架开发的时候整天都要接触配置文件,那时候的配置文件基本上都是.propertie ...