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. 个人所得税计算java版

    年关将至,该到了发年终奖的时候了.所以就到网上去找下,个税计算器,但是发现做的有点像病毒网站似的.所以计算结果也不太敢信,于是琢磨着,要不自己动手写一个个税计算器吧. 说干就干,先上国家税务局了解了下 ...

  2. SecureCRT远程连接Linux下的sqlplus中退格键不能使用之解决方法

    ^H不是H键的意思,是backspace 主要是当你的终端backspace有问题的时候才需要设置   在linux环境下使用sqlplus,在回删(backspace)时往往会出现 一串的乱码.出现 ...

  3. SpringMVC_关于<url-pattern>

    一.配置   在没有特殊要求的情况下,SpringMVC的中央调度器DispatcherServlet的<url-oattern/>常使用后缀匹配的方式,如写*do. 二.不能写为/* 这 ...

  4. HBase入门教程

    # 背景 最近看到公司一个项目用到hbase, 之前也一直想看下hbase.个人理解Hbase作为一个nosql数据库,逻辑模型感觉跟关系型数据库有点类似.一个table,有row即行,列.不过列是一 ...

  5. Install MySQL on Mac

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

  6. Android adb命令查看sharedpreferences

    adb shell run-as com.example.android //对应包名 ls查看当前目录下的所有文件,找到shared_prefs cd shared_prefs ls 查看所有的 s ...

  7. Java Basis

    java中.java源文件放在src文件夹下,.class文件放在bin文件夹下. java代码区域,以及控制台区域字体大小更改.Java->Java Editor Text Font      ...

  8. 4类Storage方案(AS开发实战第四章学习笔记)

    4.1 共享参数SharedPreferences SharedPreferences按照key-value对的方式把数据保存在配置文件中,该配置文件符合XML规范,文件路径是/data/data/应 ...

  9. 【转】c++ http下载文件

    #include <afx.h> #include <afxinet.h> #define RECVPACK_SIZE 2048 bool DownloadSaveFiles( ...

  10. spring boot(2)-@SpringBootApplication详解

    pom.xml <parent> <groupId>org.springframework.boot</groupId> <artifactId>spr ...