利用栈的操作,遇到"(","[","{"即进栈,遇到")","]","}"判断是否与栈顶匹配,若不匹配则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. nacos作为配置中心

    分布式配置中心 在微服务架构中,为什么需要一个统一的配置中心呢?如果用一句话来说那就是方便管理,降低出错的可能.比如:你开发环境是一套配置,测试环境是一套,生产环境又是一套.你如果手动去修改,难免会出 ...

  2. cocos2d windows游戏平台搭建

    1. 安装VS2013 2. 下载cocos2d源代码(cocos2d-x-3.7.1) 3. 下载和安装python(2.7.10) 4. 安装完成后,将python安装路径设置到系统路径中(pat ...

  3. CTF--web

    https://adworld.xctf.org.cn/task/task_list?type=web&number=3&grade=0 1.view source 查看源代码 1.鼠 ...

  4. loadrunner 添加集合点和添加压力机

    loadrunner 添加集合点和添加压力机 一.添加集合点: 1.在脚本中右键insert--rendezvous (集合点一定要添加在事务的外面,否则影响事务准确性) 2.创建controller ...

  5. python集合的分类与操作

    如图: 集合的炒作分类: 确定大小 测试项的成员关系 遍历集合 获取一个字符串表示 测试相等性 连接两个集合 转换为另一种类型的集合 插入一项 删除一项 替换一项 访问或获取一项

  6. I2C(二) linux2.6

    目录 I2C(二) linux2.6 总线驱动 关键结构 入口 i2c_add_adapter 硬件操作 设备驱动 入口 注册 attach_adapter eeprom_detect i2c_att ...

  7. 【Unity游戏开发】tolua之wrap文件的原理与使用

    本文内容转载自:https://www.cnblogs.com/blueberryzzz/p/9672342.html .非常感谢原作者慷慨地授权转载,比心!@blueberryzzz 是位大神,欢迎 ...

  8. Linux下批量杀掉 包含某个关键字的 程序进程

    有时候因为一些情况,需要把 linux 下符合某一项条件的所有进程 kill 掉,又不能用 killall 直接杀掉某一进程名称包含的所有运行中进程(我们可能只需要杀掉其中的某一类或运行指定参数命令的 ...

  9. package.json 里的 dependencies和devDependencies区别

    dependencies(依赖的意思): 通过 --save 安装,是需要发布到生产环境的.比如项目中使用react,那么没有这个包的依赖就会报错,因此把依赖写入dependencies devDep ...

  10. HDU-1398 Square Coins(生成函数)

    题意 与$hdu1028$类似,只不过可用的数字都是平方数. 思路 类似的思路,注意下细节. 代码 #include <bits/stdc++.h> #define DBG(x) cerr ...