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 ...
随机推荐
- 冲突--ScrollView嵌套ListView冲突问题的最优解决方案
项目做多了之后,会发现其实 ScrollView嵌套ListVew或者GridView等很常用,但是你也会发现各种奇怪问题产生.根据个人经验现在列出常见问题以及代码最少最简单的解决方法. 问题一 : ...
- LNMP环境搭建配置memcache
原始出处 http://iceeggplant.blog.51cto.com/1446843/819576 memcached是高性能的,分布式的内存对象缓存系统,在动态应用中减少数据库负载,提升访 ...
- ios统计代码行数
要统计ios开发代码,包括头文件的,终端命令进入项目目录下,命令如下 列出每个文件的行数: find . -name "*.m" -or -name "*.h" ...
- 解决脱离rails使用activerecord报错 NameError: uninitialized constant ActiveRecord::Migrator::Zlib
上下文说明 原本系统是15.10,无奈只支持1年,所以今天升级16.04,环境答好后运行rake migratte报错 task :default => :migrate desc 'Run m ...
- [Java] 读写字节数据,过滤流DataOutputStream和DataInputStream
package test.stream; import java.io.DataInputStream; import java.io.DataOutputStream; import java.io ...
- [Flex] Accordion系列 - Header图标的设置
<?xml version="1.0" encoding="utf-8"?> <!--Flex中如何通过getHeaderAt()函数以及se ...
- python 之 模拟GET/POST提交
以 POST/GET 方式向 http://127.0.0.1:8000/test/index 提交数据. # coding:utf-8 import httplib import urllib cl ...
- golang 移动应用例子 example/basic 源码框架分析
条件编译 我们在源码中可以看到2个文件: main.go 和 main_x.go 这两个包名都是 package main , 都有 main 函数. 不会冲突么? 答案是不会的, main_x.go ...
- POJ 1149 PIGS 【网络流】
题意: m n //有m个猪圈,n个人卖猪. a1...am //编号为i的猪圈里有ai头猪. b1 c1...cb1 d1 //第i个人有bi把钥匙,分别是ci猪圈的,其它猪圈里的猪都 ...
- [HDU 1011] Starship Troopers (树形dp)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1011 dp[u][i]为以u为根节点的,花了不超过i元钱能够得到的最大价值 因为题目里说要访问子节点必 ...