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

leetcode 20 括号匹配的更多相关文章

  1. leetcode 栈 括号匹配

    https://oj.leetcode.com/problems/valid-parentheses/ 遇到左括号入栈,遇到右括号出栈找匹配,为空或不匹配为空, public class Soluti ...

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

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

  3. 《LeetBook》leetcode题解(20):Valid Parentheses[E]——栈解决括号匹配问题

    我现在在做一个叫<leetbook>的免费开源书项目,力求提供最易懂的中文思路,目前把解题思路都同步更新到gitbook上了,需要的同学可以去看看 书的地址:https://hk029.g ...

  4. LeetCode 第20题--括号匹配

    1. 题目 2.题目分析与思路 3.代码 1. 题目 给定一个只包括 '(',')','{','}','[',']' 的字符串,判断字符串是否有效. 有效字符串需满足: 左括号必须用相同类型的右括号闭 ...

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

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

  6. [LeetCode]20. Valid Parentheses有效的括号

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

  7. Leetcode 20 有效的括号valid-parentheses(栈)

    给定一个只包括 '(',')','{','}','[',']' 的字符串,判断字符串是否有效. 有效字符串需满足: 左括号必须用相同类型的右括号闭合. 左括号必须以正确的顺序闭合. 注意空字符串可被认 ...

  8. 【LeetCode 20】有效的括号

    题目链接 [题解] 一道傻逼括号匹配题 [代码] class Solution { public: bool isValid(string s) { vector<char> v; int ...

  9. Valid Parentheses [LeetCode 20]

    1- 问题描述 Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if ...

随机推荐

  1. hdu 2412 Party at Hali-Bula 经典树形DP

    Party at Hali-Bula Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Other ...

  2. Linux-debian系统 /etc/network/interface 文件解读

    原文 http://wiki.slimdevices.com/index.php/SqueezeOS_networking 话说Debian系的网卡配置跟Redhat系很不一样,Redhat是放在/e ...

  3. WebService,ASMX文件使用XML格式数据传递参数、验证与获取XML格式返回值的一种方式

    1:首先WebService方法定义,每个方法定义两个参数,一个用于验证权限,string格式的XML文本用于传输数据.最终目的实现,WebService方法,验证权限,获取XML数据,处理之后返回X ...

  4. JS判断两个数是否能除尽

    function judgeDivisor(m, n) { var num = {}; var i = 0; var x = parseInt(m / n); m = m % n; var resul ...

  5. Thymeleaf学习记录(7)--页面引入/片段引入

    1.为页面添加footer: Templates文件夹下新建HTML文件: <!DOCTYPE html SYSTEM "http://www.thymeleaf.org/dtd/xh ...

  6. 关于html 中form表单的内标签和使用

    表单标记 1.普通文本框: <input type=”text” name=”名称” value=”值”;不写value默认为空/> 2.密码框:<input type=”passw ...

  7. css的高级选择器,后代选择器,子代选择器,并集选择器,交集选择器

    高级选择器: 后代选择器 子代选择器 并集选择器 交集选择器 一.后代选择器: 使用空格表示后代选择器,父元素的后代(包括儿子,孙子,从孙子) 也就是说,box类下的所有span标签 字体颜色都被设置 ...

  8. HDU P3341 Lost's revenge 题解+数据生成器

    Lost and AekdyCoin are friends. They always play "number game"(A boring game based on numb ...

  9. logback配置文件---logback.xml详解

    一.参考文档 1.官方文档 http://logback.qos.ch/documentation.html 2.博客文档 http://www.cnblogs.com/warking/p/57103 ...

  10. Install MySQL on Mac

    1. 可参考此文章:http://www.cnblogs.com/macro-cheng/archive/2011/10/25/mysql-001.html 2. 目前MySQL(我用的mysql 5 ...