描述:

给定一些列括号,判断其有效性,即左括号有对应的有括号,括号种类只为小,中,大括号。

解决:

用栈。

bool isValid(string s) {
stack<char> st;
for (auto i : s) {
if (i == '(' || i == '[' || i == '{') {
st.push(i);
continue;
} else {
if (st.empty())
return false;
char c = st.top();
if (i == ')' && c != '(' ||
i == ']' && c != '[' ||
i == '}' && c != '{')
return false;
st.pop();
}
}
if (st.empty())
return true;
return false;
}

leetcode 20 Valid Parentheses 有效的括号的更多相关文章

  1. leetCode 20.Valid Parentheses (有效的括号) 解题思路和方法

    Valid Parentheses  Given a string containing just the characters '(', ')', '{', '}', '[' and ']', de ...

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

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

  3. leetcode 20. Valid Parentheses 、32. Longest Valid Parentheses 、

    20. Valid Parentheses 错误解法: "[])"就会报错,没考虑到出现')'.']'.'}'时,stack为空的情况,这种情况也无法匹配 class Soluti ...

  4. [LeetCode] 20. Valid Parentheses 验证括号

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

  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 有效的括号

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 个人公众号:负雪明烛 本文关键词:有效,括号,括号匹配,栈,题解,leetcode, 力扣 ...

  8. leetcode 20 Valid Parentheses 括号匹配

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

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

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

随机推荐

  1. java中可以让程序暂停几秒执行的代码

    //n为毫秒数 try { Thread.sleep ( n ) ; } catch (InterruptedException ie){} try { TimeUnit.SECONDS.sleep( ...

  2. [爬虫] 学Scrapy,顺便把它的官方教程给爬下来

    想学爬虫主要是因为算法和数据是密切相关的,有数据之后可以玩更多有意思的事情,数据量大可以挖掘挖掘到更多的信息. 之前只会通过python中的request库来下载网页内容,再用BeautifulSou ...

  3. tensorflow中协调器 tf.train.Coordinator 和入队线程启动器 tf.train.start_queue_runners

    TensorFlow的Session对象是支持多线程的,可以在同一个会话(Session)中创建多个线程,并行执行.在Session中的所有线程都必须能被同步终止,异常必须能被正确捕获并报告,会话终止 ...

  4. CNN中卷积层的计算细节

    原文链接: https://zhuanlan.zhihu.com/p/29119239 卷积层尺寸的计算原理 输入矩阵格式:四个维度,依次为:样本数.图像高度.图像宽度.图像通道数 输出矩阵格式:与输 ...

  5. 抽取JDBCTemplate

    抽取JDBCTemplate 为了解决DAO实现类中代码的重复问题,另外使得代码更通用,所以抽取一个公共的模板,使其更具有通用性. DAO实现类的代码 public class StudentDAOI ...

  6. Documentation/kobject.txt翻译--sysfs

    你从未想过的关于kobjects,ksets和ktypes的一切:作者:Greg Kroah-Hartman <gregkh@linuxfoundation.org>:上次更新时间:200 ...

  7. CentOS 6.5 下keepalived服务的配置

    CentOS 6.5 下keepalived服务的配置 参考网站: http://zhangxugg-163-com.iteye.com/blog/1665419 http://www.2cto.co ...

  8. 你正在从一个声称代表如下的证书颁发机构安装证书 alipay truest network,希望能知道程序是怎么实现的或相关资料

    你正在从一个声称代表如下的证书颁发机构安装证书 alipay truest network,希望能知道程序是怎么实现的或相关资料

  9. PLSQL在64位 win7/WIN8上运行(绿色版plsql、无需安装oracle客户端)

    一.准备需要的文件: 1.plsql文件绿色压缩包 2.oracle客户端文件绿色压缩包 二.设置PLSQL 运行plsql,不登陆进入plsql点“工具”-“首选项” 按照如图设置“ oracle主 ...

  10. 【python】使用Python中的urlparse、urllib抓取和解析网页

    一.解析URL 函数urlparse(urlstring [, default_scheme [, allow_fragments]])的作用是将URL分解成不同的组成部分,它从urlstring中取 ...