LeetCode第20题:有效的括号
问题描述:
给定一个只包括 '(',')','{','}','[',']' 的字符串,判断字符串是否有效。
有效字符串需满足:
左括号必须用相同类型的右括号闭合。
左括号必须以正确的顺序闭合。
注意空字符串可被认为是有效字符串。
示例 1:
输入: "()"
输出: true
示例 2:
输入: "()[]{}"
输出: true
示例 3:
输入: "(]"
输出: false
示例 4:
输入: "([)]"
输出: false
示例 5:
输入: "{[]}"
输出: true
解题思路:
这道题不是特别难,
如果字符串为空,返回true。
先初始化一个STL的stack,左括号入栈,右括号与栈顶进行匹配,匹配的话出栈,不匹配返回false。
如果当字符串结束之后栈仍然不为空,仍然需要返回false。
如果当字符串结束之后栈为空,则返回true。
C+代码:
class Solution {
public:
bool isValid(string s) {
//空字符串有效
if(s.size()==0)
return true;
//初始化堆栈来存储各个括号
std::stack<char> brackets;
for(int noOfStr=0;noOfStr<s.size();++noOfStr){
char tempChar=s[noOfStr];
switch(tempChar){
//栈不空且栈顶为相应的左括号,栈顶出栈
//否则返回false
case ')':{
if(!brackets.empty()&&brackets.top()=='(')
brackets.pop();
else
return false;
}break;
case ']':{
if(!brackets.empty()&&brackets.top()=='[')
brackets.pop();
else
return false;
}break;
case '}':{
if(!brackets.empty()&&brackets.top()=='{')
brackets.pop();
else
return false;
}break;
//左括号入栈
default:{
brackets.push(tempChar);
}
}
}
//字符串结束后栈仍不为空,返回false
if(!brackets.empty())
return false;
else
return true;
}
};
执行结果:

LeetCode第20题:有效的括号的更多相关文章
- LeetCode 第20题--括号匹配
1. 题目 2.题目分析与思路 3.代码 1. 题目 给定一个只包括 '(',')','{','}','[',']' 的字符串,判断字符串是否有效. 有效字符串需满足: 左括号必须用相同类型的右括号闭 ...
- Leetcode 20题 有效的括号(Valid Parentheses) Java语言求解
题目描述: 给定一个只包括 '(',')','{','}','[',']' 的字符串,判断字符串是否有效. 有效字符串需满足: 左括号必须用相同类型的右括号闭合. 左括号必须以正确的顺序闭合. 注意空 ...
- leetcode第20题--Valid Parentheses
Problem: Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if ...
- LeetCode第二十题-有效的括号
Valid Parentheses 问题简介: 给定一个只包含字符 ‘(’ , ‘)’ , ‘{’ , ‘}’ , ‘[’ , ‘]’ 的字符串,确定输入字符串是否有效 有效的条件: 必须使用相同类型 ...
- LeetCode第[20]题(Java):Valid Parentheses
题目:有效的括号序列 难度:Easy 题目内容: Given a string containing just the characters '(', ')', '{', '}', '[' and ' ...
- LeetCode第20题
LeetCode20题不多说上代码 public boolean isValid(String s){ Stack<Character> stack = new Stack<Char ...
- Leetcode(20)-有效的括号
给定一个只包括 '(',')','{','}','[',']' 的字符串,判断字符串是否有效. 有效字符串需满足: 左括号必须用相同类型的右括号闭合. 左括号必须以正确的顺序闭合. 注意空字符串可被认 ...
- 【LeetCode算法-20】Valid Parentheses
LeetCode第20题 Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determin ...
- [LeetCode] 20. Valid Parentheses 验证括号
Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the inpu ...
随机推荐
- 为什么要用redux?component自身管理自己的state难道不更解耦吗?
这是前几天百度2面的时候,面试官问我的一个问题.说实话当时有点紧张,其实也没去细想,本身react我就学了2个多星期,虽然看过redux这些源码,不过这个问题好像我从来没想过. 那其实react官网本 ...
- Ansible 实战之部署Web架构
WEB架构(ubuntu 16.04): Proxy -- WebServer(Nginx+PHP+Django) -- Nosql -- MariaDB 一. 定义Inventory [proxy] ...
- SrpingCloud 之SrpingCloud config分布式配置中心搭建
1.搭建git环境 目的:持久化存储配置文件信息 采用码云 创建后 继续创建文件夹 用来区分不同的项目 下面就是git上存放配置文件了.环境的区分 dev sit pre prd 开发 ...
- Netty入门例子
新建maven项目,添加依赖 <!-- https://mvnrepository.com/artifact/io.netty/netty-all --> <dependency&g ...
- ubantu删除文件(夹)
格式:rm -rf 目录名字 -r 就是向下递归,不管有多少级目录,一并删除 -f 就是直接强行删除,不作任何提示的意思 名称 rm - 移除文件或者目录 概述 rm [选项]... 文件列表... ...
- node向html模板发送数据
node向html模板发送数据 给模板传递数据 router.get('/', function(req, res, next) { res.render('index', { title: '张三' ...
- BZOJ 3433 [Usaco2014 Jan]Recording the Moolympics:贪心
题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=3433 题意: 给出n个区间[a,b). 有两个记录器,每个记录器中存放的区间不能重叠. 求 ...
- js操作url的常用函数
1. //替换指定传入参数的值,paramName为参数,replaceWith为新值 function replaceParamVal(oUrl,paramName, replaceWith) { ...
- python 面试题(一)
1 Python的函数参数传递 看两个例子: Python 1 2 3 4 5 a = 1 def fun(a): a = 2 fun(a) print a # 1 Python ...
- ES field store yes no 区别——可以设置为false,如果_source有的话
store By default, field values are indexed to make them searchable, but they are not stored. This me ...