https://leetcode.com/problems/valid-parentheses/

原题:

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

The brackets must close in the correct order, "()" and "()[]{}" are all valid but "(]" and "([)]" are not.

思路:

考察stack的应用。

我的AC代码:

 class Solution {
public:
bool isValid(string s) {
int n=s.size();
if(n%==)
return false;
stack<char> a; //push,pop,top,size
for(int i=;i<n;i++){
if(s[i]=='('|| s[i]=='['||s[i]=='{')
a.push(s[i]);
else if (a.empty()==false &&( (a.top()=='(' && s[i]==')') || (a.top()=='[' && s[i]==']') || (a.top()=='{' && s[i]=='}') ) )
a.pop();
else
return false;
}
if(a.empty())
return true;
else return false;
}
};

LeetCode题解(20)--Valid Parentheses的更多相关文章

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

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

  2. [Leetcode][Python]20: Valid Parentheses

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 20: Valid Parentheseshttps://oj.leetcod ...

  3. C# 写 LeetCode easy #20 Valid Parentheses

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

  4. leetcode个人题解——#20 Valid Parentheses

    class Solution { public: bool isValid(string s) { stack<char> brackts; ; i < s.size(); i++) ...

  5. 【LeetCode】20. Valid Parentheses 有效的括号

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

  6. 【一天一道LeetCode】#20. Valid Parentheses

    一天一道LeetCode系列 (一)题目 Given a string containing just the characters '(', ')', '{', '}', '[' and ']', ...

  7. [LeetCode 题解]: Valid Parentheses

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

  8. LeetCode:20. Valid Parentheses(Easy)

    1. 原题链接 https://leetcode.com/problems/valid-parentheses/description/ 2. 题目要求 给定一个字符串s,s只包含'(', ')',  ...

  9. leetcode题解:Valid Parentheses(栈的应用-括号匹配)

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

随机推荐

  1. 【编程工具】Sublime Text3快捷键配置

    我们在使用编译软件时,总是喜欢使用快捷键来方便我们的操作,但有些编译软件不支持快捷键的修改和设置,为了能够更加方便的使用 Sublime Text3,这里我介绍几个 Sublime Text3 设置快 ...

  2. pytion3--文档字符串:__doc__

    除了#注释外,Python也支持可自动附加在对象上的文档,而且在运行时还可保存查看.从语法上来说,这类注释是写成字符串,放在模块文档.函数以及类语句的顶端.就在任何可执行程序代码前(#注释在其前也没问 ...

  3. 一堆Offer怎么选?这样做就不纠结了

    有个朋友,工作了10年左右,春节后换工作,拿了三个Offer(西安): 通信行业的一家研究所,软件开发工程师,月薪7K,承诺有月奖金.年终奖金 一家做大数据的公司,软件开发工程师,月薪15K,13薪 ...

  4. 【Luogu】P3381最小费用最大流模板(SPFA找增广路)

    题目链接 哈  学会最小费用最大流啦 思路是这样. 首先我们有一个贪心策略.如果我们每次找到单位费用和最短的一条增广路,那么显然我们可以把这条路添加到已有的流量里去——不管这条路的流量是多大,反正它能 ...

  5. BZOJ2561 最小生成树 【最小割】

    题目 给定一个边带正权的连通无向图G=(V,E),其中N=|V|,M=|E|,N个点从1到N依次编号,给定三个正整数u,v,和L (u≠v),假设现在加入一条边权为L的边(u,v),那么需要删掉最少多 ...

  6. eclipse导入svn检出的maven项目问题

    1.修改项目jdk环境和编译环境.消除红叉. 2.windows-preferences-java-installed jres,修改工作空间的jdk,在Default vm arguments栏中添 ...

  7. bzoj3000 Big Number 数论,斯特林公式

    Description 给你两个整数N和K,要求你输出N!的K进制的位数. Input 有多组输入数据,每组输入数据各一行,每行两个数——N,K Output 每行一个数为输出结果 Sample In ...

  8. h5表单验证的css和js方法

    1.css3 提示只适用于高级浏览器: Chrome,Firefox,Safari,IE9+ valid.invalid.required的定义 代码如下: input:required, input ...

  9. Java的常用对象

    PO:持久对象 (persistent object),po(persistent object)就是在Object/Relation Mapping框架中的Entity,po的每个属性基本上都对应数 ...

  10. CentOS 7.5 安装Docker 教程

    Docker简介 Docker是一个开源的容器引擎,它有助于更快地交付应用.Docker可将应用程序和基础设施层隔离,并且能将基础设施当作程序一样进行管理. 使用Docker可更快地打包.测试以及部署 ...