判断括号是否合法

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. PHP各种缓存

    缓存类型PHP缓存包括PHP变异缓存和PHP数据缓存两种:PHP编译缓存:        PHP数据缓存:    对数据库数据进行缓存    对PHP模板数据缓存 js数据缓存

  2. Java web 之表单验证

    按照软件工程师的定位来讲,表单验证应该要好好练习的 html  javascript

  3. AFNetwork 作用和用法详解

    转自:http://www.cnblogs.com/mkai/p/5729685.html AFNetworking是一个轻量级的iOS网络通信类库.它建立在NSURLConnection和NSOpe ...

  4. ABP的Zero Sample

    下载自:https://github.com/aspnetboilerplate/module-zero 打开D:\ABP\module-zero-master\sample里的ModuleZeroS ...

  5. TNetHTTPClient演示

    TNetHTTPClient演示 TNetHTTPClient是DELPHI新增加的异步HTTP通信控件(区别于INDY的阻塞控件). unit Unit1; interface uses Winap ...

  6. mORMot使用基础

    mORMot 名称来自Marmot,对,土拨鼠,俗称旱獭,是一种生活在高寒山区的动物.多数都在白天活动,喜群居,善掘土,所挖地道深达数米,内有铺草的居室,非常舒适.通常洞穴都会有两个以上的入口,以策安 ...

  7. 【Java学习笔记】Map集合的keySet,entrySet,values的用法例子

    import java.util.Collection; import java.util.HashMap; import java.util.Iterator; import java.util.M ...

  8. Unity5 如何做资源管理和增量更新

    工具 Unity 中的资源来源有三个途径:一个是Unity自动打包资源,一个是Resources,一个是AssetBundle. Unity自动打包资源是指在Unity场景中直接使用到的资源会随着场景 ...

  9. 2600: [Ioi2011]ricehubh

    Description 乡间有一条笔直而长的路称为"米道".沿着这条米道上 R 块稻田,每块稻田的坐标均为一个 1 到 L 之间(含 1 和 L)的整数.这些稻田按照坐标以不减的顺 ...

  10. Intellij IDEA 根据数据库自动生成pojo和hbm

    新建一个项目,每次写hibernate部分,就觉得pojo和hbm.xml部分很蛋疼.今天搜索了半天,终于知道如何根据数据库自动生成了. Intellij IDEA14创建maven时并不能勾选各种支 ...