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

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

   前括号一律入栈,后括号如果跟栈顶符号匹配,栈顶符号出栈如果,若不匹配则返回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. STM32中断控制及优先级设置

    M3用8bits而STM32用高四位来表示抢占和子优先级:bit=1表示抢占:bit=0表示非抢占即子优先级:所以共有5中方案分组: 分组 Bit7 Bit6 Bit5 Bit4 说明: 第0组   ...

  2. eclipse中安装tomcat插件

    一.软件下载 Eclipse3.6 IDE for Java EE Developers: 下载地址:http://eclipse.org/downloads/ Tomcat Eclipse Plug ...

  3. android之AutoCompleteTextView控件用法

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools=&q ...

  4. dom4j-full.jar 解析 XML

    dom4j-full.jar 解析 XML public Document getDocument() throws DocumentException { SAXReader read=new SA ...

  5. EventBroker

    Sample publisher Publish an event topic: ? 1 2 3 4 5 6 7 8 9 10 public class Publisher {     [EventP ...

  6. Jmeter+Jenkins集成html报告显示问题

    在J meter+Jenkins集成生成的html报告时,出现如下问题:无法正常显示表格,detail内容无法折叠和展开操作. 解决方法:执行下列脚本行 在Jenkins :系统管理--脚本命令行 S ...

  7. MongoDB Tool

    robomongo MongoBooster: [推薦]MongoChef:http://3t.io/mongochef/download/ MongoVUE 是个比较好用的MongoDB客户端,不过 ...

  8. 89、Android EditText 悬浮停靠

    package com.willen.topFloatDemo; import android.content.Context; import android.os.Handler; import a ...

  9. [C#常用代码]如何把指定文件夹中的文件移动到指定的文件夹

    using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...

  10. Redis单机版安装与部署

    Redis官网:http://redis.io 下载Redis wget https://github.com/antirez/redis/archive/3.0.0-rc1.tar.gz 解压并安装 ...