利用栈的操作,遇到"(","[","{"即进栈,遇到")","]","}"判断是否与栈顶匹配,若不匹配则false。

class Solution {
public:
bool isValid(string s) {
stack<char> c;
for(int i=;i<s.size();i++){
if(!c.empty()){
if(s[i]=='('||s[i]=='{'||s[i]=='[')
c.push(s[i]);
else if(s[i]==')'){
if(c.top()=='(') c.pop();
else return false;
}
else if(s[i]=='}'){
if(c.top()=='{') c.pop();
else return false;
}
else if(s[i]==']'){
if(c.top()=='[') c.pop();
else return false;
}
}
else c.push(s[i]);
}
if(!c.empty()) return false;
return true;
}
};

LeetCode 20. Valid Parentheses(c++)的更多相关文章

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

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

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

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

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

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

  4. [leetcode]20. Valid Parentheses有效括号序列

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

  5. leetCode 20.Valid Parentheses (有效的括号) 解题思路和方法

    Valid Parentheses  Given a string containing just the characters '(', ')', '{', '}', '[' and ']', de ...

  6. leetcode 20 Valid Parentheses 括号匹配

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

  7. [LeetCode] 20. Valid Parentheses ☆

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

  8. LeetCode 20 -- Valid Parentheses

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

  9. Java [leetcode 20]Valid Parentheses

    题目描述: Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if th ...

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

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

随机推荐

  1. Activiti6-FormService(学习笔记)重要

    设置流程定义文件: <?xml version="1.0" encoding="UTF-8"?> <definitions xmlns=&qu ...

  2. Laravel数据库迁移

    Laravel的数据迁移功能很好用,并且可以带来一系列好处.通过几条简单的 artisan 命令,就可以顺利上手,没有复杂的地方 注意:该系列命令对数据库非常危险,请准备一个单独的数据库作为配套练习, ...

  3. ubuntu下python跑任务输出到文件中遇到的一些问题(输出重定向)

    之前主要是参考https://www.cnblogs.com/chason95/articles/9760291.html 一般使用 python test.py > ./log.txt 或 p ...

  4. Java 获取指定日期范围内的每个月,每季度,每一年

    /**     *根据时间范围获得月份集     * @return     */    public static List<String> getRangeSet(String beg ...

  5. 洛谷P1197 [JSOI2008]星球大战

    题目 由于题目不要求强制在线,所以可以离线. 而离线的话就会带来许多便利,所以我们可以先处理出全部打击后的图,通过并查集来判断是否连通. 然后再从后往前枚举,得出答案 #include <bit ...

  6. BZOJ 1855 股票交易 (算竞进阶习题)

    单调队列优化dp dp真的是难..不看题解完全不知道状态转移方程QAQ 推出方程后发现是关于j,k独立的多项式,所以可以单调队列优化.. #include <bits/stdc++.h> ...

  7. opencontrail—VXLAN模式下数据包的传输过程

    在这篇文章中,我们将看到VM生成的数据包如何能够到达另一个VM或外部资源,Neutron使用OpenContrail插件的上下文中的关键概念/组件是什么. 我们将重点介绍OpenContrail,它如 ...

  8. Sublime Text 3 安装简记

    1.下载:( Sublime Text Version 3.1.1 Build 3176 ) https://www.sublimetext.com/3 2.安装Package Control: &q ...

  9. GWAS:拒绝假阳性之case和control数量比例严重失衡的解决方案(SAIGE模型的应用)

    一.为什么要校正case和control数量比例不平衡情况 试问作为生信届人员,最怕的是什么,当然是统计结果不靠谱.统计结果不靠谱包括两方面:一个是假阴性,一个是假阳性.假阴性可以理解为白天鹅被误当成 ...

  10. PTA编译总结求最大值及其下标

    代码: #include<stdio.h>    int main(void)    {    int i,index=0,n;    int a[10];    scanf(" ...