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.

题意:给定字符串判断是否为合法的括号对

思路:利用栈结构,遇到左括号((、[、{)都压入栈中,遇到右括号了,若栈为空,则肯定不合法;若栈不为空,则看其是否和栈顶字符向匹配。这里以最后栈是否为空来判断整个字符串是否合法,不然类似于((( [ ] )),就会合法的了。代码如下:

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

括号的题还有generate parentheses 、longest valid parentheses

[Leetcode] valid parentheses 有效括号对的更多相关文章

  1. [LeetCode] Valid Parentheses 验证括号

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

  2. LeetCode Valid Parentheses 有效括号

    class Solution { public: void push(char c){ //插入结点 struct node *n=new struct node; n->nex=; n-> ...

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

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

  6. [LeetCode] Generate Parentheses 生成括号

    Given n pairs of parentheses, write a function to generate all combinations of well-formed parenthes ...

  7. [LintCode] Valid Parentheses 验证括号

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

  8. LeetCode: Valid Parentheses 解题报告

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

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

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

随机推荐

  1. 追书神器API

    由于自己喜欢看小说,有的时候不方便手机看的时候希望在电脑上面看,但很多网站有广告啊,于是封装了套手机版的追书神器API 目前只做了搜索 详情 书评 换源 正文 调用方式: //搜索小说 var sea ...

  2. 微信小程序—day03

    昨日问题 接着上一篇,昨天遇到的scroll-view组件不能滚动的问题. 今天经过调试,发现是由于:图片的实际宽高,大于给image设定的宽高导致的. 解决办法: 减小图片的实际宽高,使之小于ima ...

  3. TW实习日记:第29-30天

    这两天挺忙,赶工期,改bug.项目现场的同事说客户火大得不行.可是谁叫你们谈工期谈的这么紧,完全不考虑开发的情况,真的是烦人这种事情.这两天遇到的最有难度的一个点就是附件预览,搞这个改到晚上11点. ...

  4. 【springmvc+mybatis项目实战】杰信商贸-2.数据库配置

    首先我们来了解项目的架构 我们分别使用了MySql和Oracle数据库,即是异构数据库.我们做到一个平台支持多个数据库.数据库建模我们使用Sybase公司的PowerDesigner(以后简称PD), ...

  5. 树(Tree,UVA 548)

    题目描述: 题目思路: 1.使用数组建树 //递归 2.理解后序遍历和中序遍历,建立左右子树 3.dfs深度搜索找出权重最小的路径 #include <iostream> #include ...

  6. django request bug

    bug描述:django请求request接收数据时,如果参数中包含分号时,会导致分号后面的消息丢失. 比如前台js调用代码 $.post('/get_params', { "A" ...

  7. Centos6配置开启FTP Server

    vsftpd作为FTP服务器,在Linux系统中是非常常用的.下面我们介绍如何在centos系统上安装vsftp. 什么是vsftpd vsftpd是一款在Linux发行版中最受推崇的FTP服务器程序 ...

  8. POJ 3675 Telescope(简单多边形和圆的面积交)

    Description Updog is watching a plane object with a telescope. The field of vision in the telescope ...

  9. 在Excel里面,单元格里输入公式后只显示公式本身,不显示结果,怎么办

    这种情况是对Excel进行了设置,设置的就是在单元格中只显示公式,不显示结果,解决的办法有两个: 1 用快捷键CTR+~ 2 点击"公式"选项卡,然后反选里面的"显示公式 ...

  10. Bacon's Cipher(培根密码)

    Description Bacon's cipher or the Baconian cipher is a method of steganography (a method of hiding a ...