判断括号是否合法

1.用stack存入左括号“([{”,遇到相应的右括号“)]}”就退栈

2.判断stack是否为空,可以判断是否合法

 class Solution {
public:
bool isValid(string s) {
stack<char> sc;
char in[] ="([{";
char out[] =")]}";
for(string::size_type i = ; i < s.size(); ++i){
for(int j = ; j < ; ++j){
if(in[j] == s[i]) sc.push(s[i]);
}
for(int j = ; j < ; ++j){
if(out[j] == s[i]){
if(sc.empty()) return false;
else if(sc.top() == in[j]) sc.pop();
else return false;
}
}
}
return sc.empty();
}
};

Leetcode 20 Valid Parentheses stack的应用的更多相关文章

  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(Stack)

    括号匹配问题,使用栈的特点,匹配则出栈,否则入栈,最后栈为空则全部匹配.代码如下: class Solution { public: bool isValid(string s) { stack< ...

  7. leetcode 20 Valid Parentheses 括号匹配

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

  8. [LeetCode] 20. Valid Parentheses ☆

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

  9. LeetCode 20 -- Valid Parentheses

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

随机推荐

  1. hdu 1577 WisKey的眼神 (数学几何)

    WisKey的眼神 Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total S ...

  2. K.O. ----- bat文件的中文乱码

    -------siwuxie095 bat文件在保存时如果没有选择正确的格式,中文部分就会出现乱码 1.记事本 用记事本编写如下代码:        另存为:测试.bat,编码设置为:UTF-8,就会 ...

  3. Tornado 异步客户端

    前言 Tornado是很优秀的非阻塞式服务器,我们一般用它来写Web 服务器,据说知乎就是用Tornado写的. 如果对tornado源码不是很了解,可以先看一下另一篇文章: http://yunji ...

  4. win7添加鼠标右键关联

    背景: hfs升级把右键给升没了 搜索了下 都是如何删除的  但没有怎么添加 添加: 保存如下内容为.reg 然后双击导入即可 Windows Registry Editor Version 5.00 ...

  5. spring访问静态资源文件

    用 Spring MVC 开发应用程序,对于初学者有一个很头疼的问题,那就是程序数据都已经查询出来了,但界面样式仍然十分丑陋,加载不了 css,js,图片等资源文件.当你在浏览器上直接输入某个css文 ...

  6. 检查python模块是否成功安装

    例如,检查HTMLTestRunner模块是否成功安装(血淋淋的例子) 一个模块未成功安装,在直接运行python程序是不会报错,但是会提示类似于以下的错误 AttributeError: 'xxxx ...

  7. 20145225《Java程序设计》 第9周学习总结

    20145225<Java程序设计> 第9周学习总结 教材学习内容总结 第十六章 整合数据库 16.1JDBC JDBC是用于执行SQL的解决方案,开发人员使用JDBC的标准接口,数据库厂 ...

  8. Collections类常用方法总结

    1. sort 对集合进行排序 public static <T extends Comparable<? super T>> void sort(List<T> ...

  9. git -- 如何撤销本地工作目录的修改

    git checkout -- 文件名(包含路径) 撤销本地全部修改 git checkout .

  10. json jackson

    1.引入依赖 <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId&g ...