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 ...
随机推荐
- DataGridView操作
C# DataGridView控件动态添加新行 DataGridView控件在实际应用中非常实用,特别需要表格显示数据时.可以静态绑定数据源,这样就自动为DataGridView控件添加相应的行.假如 ...
- iis7设置404页面不生效的原因
打开web.config <httpErrors errorMode="Custom" existingResponse="PassThrough"> ...
- jquery 2.0.3代码结构
(function(){ (21 , 94) 定义了一些变量和函数 jQuery = function(){}; (96 , 283) 给JQ对象,添加一些方法和属性 (285 , 347) exte ...
- 20145305 《Java程序设计》第2周学习总结
教材学习内容总结 1.boolean类型可表示true与false 2.储存值超类型范围会出现溢值 3.变量在命名时不可以使用数字作为开头,不可以使用一些特殊字符,不可以与JAVA关键词同名,不可以与 ...
- [AIR] AS3.0设置屏保功能
package com.controls { import flash.desktop.NativeApplication; import flash.events.Event; import fla ...
- [Flex] IFrame系列 —— 在flex的web应用中嵌入html的方法
在flex的web应用中,我们往往必须有嵌入html的需求,这时候你会发现IFrame很有用! flex而且可以和html中的JavaScript进行交互,flex可以通过iframe调用到html中 ...
- eclipse快速收缩展开代码
快速折叠所有代码和展开所有代码都是CTRL+SHIFT+/
- java生产随机字符串
public static String getRandomString(int length) { //length表示生成字符串的长度 String base = "abcdefghij ...
- java利用过滤器实现编码的转换,内容输出的替换
在页面建个表单 <form action="login.do" method="post"> <input type="text&q ...
- python datetime时区转换
from pytz import timezone def datetime_as_timezone(date_time, time_zone): tz = timezone(time_zone) u ...