leetcode 20

判断括号的顺序是否正确;
思路:用一个堆栈来存储符号序列,按照符号匹配规则进行堆栈操作;
前括号一律入栈,后括号如果跟栈顶符号匹配,栈顶符号出栈如果,若不匹配则返回false;
最后栈为空返回true,否则返回false;
代码如下:
class Solution {
public:
bool isValid(string s) {
int n = s.length();
if(n% != )
{
return false;
}
vector<char> stack;
if(s[] != '(' && s[] != '[' && s[] != '{')
{
return false;
}
else
{
stack.push_back(s[]);
}
int j = ;
for(int i = ; i < n; ++i)
{
if(s[i] == '(' || s[i] == '[' || s[i] == '{')
{
stack.push_back(s[i]);
}
else if(s[i] == ')')
{
if(stack.back() == '(')
{
stack.pop_back();
}
else
{
return false;
}
}
else if(s[i] == ']')
{
if(stack.back() == '[')
{
stack.pop_back();
}
else
{
return false;
}
}
else if(s[i] == '}')
{
if(stack.back() == '{')
{
stack.pop_back();
}
else
{
return false;
}
}
}
if(stack.empty())
{
return true;
}
else
{
return false;
}
}
};
leetcode 20的更多相关文章
- [LeetCode] 20. Valid Parentheses 验证括号
Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the inpu ...
- LeetCode 20. 有效的括号(Valid Parentheses)
20. 有效的括号 20. Valid Parentheses 题目描述 给定一个只包括 '(',')','{','}','[',']' 的字符串,判断字符串是否有效. 有效字符串需满足: 左括号必须 ...
- Java实现 LeetCode 20 有效的括号
20. 有效的括号 给定一个只包括 '(',')','{','}','[',']' 的字符串,判断字符串是否有效. 有效字符串需满足: 左括号必须用相同类型的右括号闭合. 左括号必须以正确的顺序闭合. ...
- Valid Parentheses [LeetCode 20]
1- 问题描述 Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if ...
- leetcode 20. Valid Parentheses 、32. Longest Valid Parentheses 、
20. Valid Parentheses 错误解法: "[])"就会报错,没考虑到出现')'.']'.'}'时,stack为空的情况,这种情况也无法匹配 class Soluti ...
- [LeetCode] 20. Valid Parentheses ☆
转载:https://leetcode.windliang.cc/leetCode-20-Valid%20Parentheses.html 描述 Given a string containing j ...
- LeetCode 20 Valid Parentheses (括号匹配问题)
题目链接 https://leetcode.com/problems/valid-parentheses/?tab=Description Problem: 括号匹配问题. 使用栈,先进后出! ...
- [LeetCode] 20. 有效的括号 (栈)
思路: 首先用字典将三对括号存储,遍历字符串中每个字符,遇到左括号就入栈:遇到右括号就开始判断:是否与栈弹出的顶字符相同. 如果到最后栈被清空,说明全部匹配上了,为真. class Solution( ...
- [LeetCode] 20. Valid Parentheses 合法括号
Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the inpu ...
- LeetCode 20 -- Valid Parentheses
Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the inpu ...
随机推荐
- suibi11172
http://blog.sina.com.cn/s/blog_12f37f0f00102wi8q.html 七.Python列表操作的函数和方法 列表操作包含以下函数: 1.cmp(list1, li ...
- java_ _反射
Java语言的反射机制初步学习 首先看下基本概念: (一)在Java运行时环境中,对于任意一个类,能否知道这个类有哪些属性和方法?对于任意一个对象,能否调用它的任意一个方法?答案是肯定的.这种动态获取 ...
- [ActionScript 3.0] AS3 实现XML转换成JSON
package com.fylib.util { /** * @author Frost.Yen * @E-mail 871979853@qq.com * @create 2015-6-18 下午2: ...
- testng.xml创建及解析
项目右键---TestNG -----> Convert to TestNG 会自动产生一个testng.xml的文件 http://www.cnblogs.com/choosewang/art ...
- java类包第十一章
1.同一个包中的类互相访问,不需要制定包名. 2.java中包名的规则是全部使用小写字母 3.final 方法不能被覆盖, public class OuterClass { innerClas ...
- Mplayer 官方中文手册
MPlayer 名称总览描述交互式控制用法配置文件配置集通用选项播放器选项(仅适用于MPLAYER)分离器/媒体流选项OSD/字幕选项音频输出选项(仅适用于MPLAYER)音频输出驱动(仅适用于MPL ...
- NSUrl 的常见用法
NSURL *url = [NSURL URLWithString:@"http://www.baidu.com/s?tn=baiduhome_pg&bs=NSRUL&f=8 ...
- Hive基础之Hive开启查询列名及行转列显示
Hive默认情况下查询结果里面是只显示值: hive> select * from click_log; OK ad_101 :: ad_102 :: ad_103 :: ad_104 :: a ...
- 使用kendoui对grid指定行变色
关键点在于绑定数据源后进行判断,可直接获取当前绑定对象的属性 dataBound: function () { dataView = this.dataSource.view(); ; i < ...
- 【转】一个URL编码和解码的C++类
下面的代码实现了一个用于C++中转码的类strCoding.里面有UTF8.UNICODE.GB2312编码的互相转换. .H文件: #pragma once #include <iostrea ...