判断括号的顺序是否正确;

思路:用一个堆栈来存储符号序列,按照符号匹配规则进行堆栈操作;

   前括号一律入栈,后括号如果跟栈顶符号匹配,栈顶符号出栈如果,若不匹配则返回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的更多相关文章

  1. [LeetCode] 20. Valid Parentheses 验证括号

    Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the inpu ...

  2. LeetCode 20. 有效的括号(Valid Parentheses)

    20. 有效的括号 20. Valid Parentheses 题目描述 给定一个只包括 '(',')','{','}','[',']' 的字符串,判断字符串是否有效. 有效字符串需满足: 左括号必须 ...

  3. Java实现 LeetCode 20 有效的括号

    20. 有效的括号 给定一个只包括 '(',')','{','}','[',']' 的字符串,判断字符串是否有效. 有效字符串需满足: 左括号必须用相同类型的右括号闭合. 左括号必须以正确的顺序闭合. ...

  4. Valid Parentheses [LeetCode 20]

    1- 问题描述 Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if ...

  5. leetcode 20. Valid Parentheses 、32. Longest Valid Parentheses 、

    20. Valid Parentheses 错误解法: "[])"就会报错,没考虑到出现')'.']'.'}'时,stack为空的情况,这种情况也无法匹配 class Soluti ...

  6. [LeetCode] 20. Valid Parentheses ☆

    转载:https://leetcode.windliang.cc/leetCode-20-Valid%20Parentheses.html 描述 Given a string containing j ...

  7. LeetCode 20 Valid Parentheses (括号匹配问题)

    题目链接 https://leetcode.com/problems/valid-parentheses/?tab=Description   Problem: 括号匹配问题. 使用栈,先进后出!   ...

  8. [LeetCode] 20. 有效的括号 (栈)

    思路: 首先用字典将三对括号存储,遍历字符串中每个字符,遇到左括号就入栈:遇到右括号就开始判断:是否与栈弹出的顶字符相同. 如果到最后栈被清空,说明全部匹配上了,为真. class Solution( ...

  9. [LeetCode] 20. Valid Parentheses 合法括号

    Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the inpu ...

  10. LeetCode 20 -- Valid Parentheses

    Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the inpu ...

随机推荐

  1. iframe替代方案

    自己写一个pagelet框架.封装成JSP的Taglib. <tms:view header="common-header" footer="common-foot ...

  2. JavaScript表单验证实例

    1. 长度限制<script>function test(){if(document.a.b.value.length>50){alert("不能超过50个字符!" ...

  3. JDBC常用代码

    try { //加载驱动 Class.forName("com.mysql.jdbc.Driver"); String url="jdbc:mysql://127.0.0 ...

  4. URL编码CFURLCreateStringByAddingPercentEscapes使用(ARC)

    URL 编码:CFURLCreateStringByAddingPercentEscapes If you have tried to send any information using a GET ...

  5. Visual Studio 2015正式版/产品密钥 Win10正式版官方原版ISO镜像下载大全&安装激活教程

    Visual Studio 2015  发行说明: https://visualstudio.com/zh-cn/news/vs2015-vs.aspx Visual Studio  2015 特性简 ...

  6. [SQL]LTRIM 、 RTRIM、SUBSTRING 如何使用

    (一) LTRIM ( character_expression )删除字符变量中的起始空格 RTRIM ( character_expression ) 删除字符变量中的尾随空格 (二) SUBST ...

  7. mysql start server faild

    可能没卸载干净...在安装mysql数据库时,如果重新安装,很容易遇见apply security setting error,即在配置mysql启动服务时,在启动apply security set ...

  8. 20. Valid Parentheses(stack)

    Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the inpu ...

  9. isAnagram

    /*Given two strings s and t, write a function to determine if t is an anagram of s. For example, s = ...

  10. Ubuntu12.04配置静态ip地址

    Ubuntu12.04配置静态ip地址 $sudo gedit /etc/network/interfaces 原有内容只有如下两行: auto lo iface lo inet loopback 向 ...