判断括号是否合法

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. Activity生命周期(一) 暨 帮助文档的使用

    --------siwuxie95 首先创建一个ActivityLifeCircle 选择API:21   Android 5.0  (截止目前:2016/12/21,承上启下,兼容更好) 选择空活动 ...

  2. Python全栈--7.1--字符串的格式化

    Python字符串格式化:(百分号/format) 1.百分号的方式: %[(name)][flags][width].[precision]typecode (name)      可选,用于选择指 ...

  3. 如何在spark中读写cassandra数据 ---- 分布式计算框架spark学习之六

    由于预处理的数据都存储在cassandra里面,所以想要用spark进行数据分析的话,需要读取cassandra数据,并把分析结果也一并存回到cassandra:因此需要研究一下spark如何读写ca ...

  4. Java.web-application-development-environments-for-macosx

    Java Web Application开发 1 下载需要的软件 使用的软件并没有采用最新的版本,只是采用了次新版本. 1.1 下载Eclipse的jee版本 eclipse-jee-luna-SR2 ...

  5. mysql 日志

    1.error_log 记录mysql的启动关闭的信息 记录mysql服务器运行错误的信息 记录mysql的表检查或修复信息 路径:my.cnf中通过--log-error=[file_name]配置 ...

  6. acm入门编成题

    http://wenku.baidu.com/view/c8f2f64acf84b9d528ea7aee.html

  7. MVC+ajax权限管理

    不喜欢说废话,直接了当: 1.控制器 /// <summary> /// 获取列表 /// </summary> /// <returns></returns ...

  8. 专题:mdadm Raid & LVM

    >FOR FREEDOM!< {A} Introduction Here's a short description of what is supported in the Linux R ...

  9. atexit函数和两种特殊文件权限位

    atexit函数 atexit函数的原型如下 void atexit(void (*func)(void)) 它是一个参数为返回值和参数均为空的函数指针的函数,含义是当前进程结束之前执行参数函数指针所 ...

  10. Windows 打印控件

    Windows窗体的PrintDocument组件用于设置一些属性,这些属性说明,在基于Windows的应用程序中要打印说明内容以及打印文档的能力,可将它与PrintDialog组件一起使用来控制文档 ...