给定一个只包括 '('')''{''}''['']' 的字符串,判断字符串是否有效。

有效字符串需满足:

  1. 左括号必须用相同类型的右括号闭合。
  2. 左括号必须以正确的顺序闭合。

注意空字符串可被认为是有效字符串。

示例 1:

输入: "()"
输出: true

示例 2:

输入: "()[]{}"
输出: true

示例 3:

输入: "(]"
输出: false

示例 4:

输入: "([)]"
输出: false

示例 5:

输入: "{[]}"
输出: true
 bool isValid(string s)
{
stack<char> Stack;
for(auto &c:s)
{
switch(c)
{
case '(':
case '[':
case '{':
Stack.push(c);
break;
case ')':
if(Stack.empty()||Stack.top()!='(')
return false;
else
Stack.pop();
break;
case ']':
if(Stack.empty()||Stack.top()!='[')
return false;
else
Stack.pop();
break;
case '}':
if(Stack.empty()||Stack.top()!='{')
return false;
else
Stack.pop();
break;
}
}
if(!Stack.empty())
return false;
else
return true;
}

LeetCode 20. 有效的括号( 括号配对 )的更多相关文章

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

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

  2. [LeetCode] 20. Valid Parentheses 合法括号

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

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

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

  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 '(', ' ...

  6. 【LeetCode 20】有效的括号

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

  7. Leetcode 20题 有效的括号(Valid Parentheses) Java语言求解

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

  8. Leetcode 856. Score of Parentheses 括号得分(栈)

    Leetcode 856. Score of Parentheses 括号得分(栈) 题目描述 字符串S包含平衡的括号(即左右必定匹配),使用下面的规则计算得分 () 得1分 AB 得A+B的分,比如 ...

  9. 9.9递归和动态规划(六)——打印n对括号的所有有效组合(即左右括号正确配对)

    /**  * 功能:打印n对括号的所有有效组合(即左右括号正确配对). */ 两种方法: 方法一: /** * 思路:在括号的最前面或者原有的每对括号中面插入一对括号. 至于其它任何位置.比方字符串的 ...

  10. 每日一道 LeetCode (6):有效的括号

    每天 3 分钟,走上算法的逆袭之路. 前文合集 每日一道 LeetCode 前文合集 代码仓库 GitHub: https://github.com/meteor1993/LeetCode Gitee ...

随机推荐

  1. android端 socket长连接 架构

    看过包建强的<App研发录>之后对其中的基础Activity类封装感到惊讶,一直想找一种方式去解决关于app中使用socket长连接问题,如何实现简易的封装来达到主活动中涉及socket相 ...

  2. ES5与ES6的研究

    今天开始ES5与ES6的研究. 1.什么是ES5与ES6? 就是ECMAScript的第五个版本与第六个版本,那么问题来了,什么是ECMAScript?首先它是一种由Ecma国际通过ECMA-262标 ...

  3. Google开发者大会:你不得不知的Tensorflow小技巧

    Google开发者大会:你不得不知的Tensorflow小技巧 同步滚动:开   Google Development Days China 2018近日在中国召开了.非常遗憾,小编因为不可抗性因素滞 ...

  4. 使用open live writer客户端写博客

    注:Windows Live Writer 已经停止更新,建议安装 Open Live Writer,下载地址: http://openlivewriter.org/ 使用open live writ ...

  5. Oracle 查询优化的基本准则详解

      注:报文来源:想跌破记忆寻找你 < Oracle 查询优化的基本准则详解 > Oracle 查询优化的基本准则详解 1:在进行多表关联时,多用 Where 语句把单个表的结果集最小化, ...

  6. 给div拼接html 拼接字符串

    简单描述:拼接html 拼接字符串,说实话,拼接这种东西我自己弄,得花费很多时间,主要是转义字符,单引号,双引号这种小细节调整起来比较麻烦,一旦疏忽多了少了一个符号,页面就有点抽象了,我呢比较粗枝大叶 ...

  7. mysql通配符使用

    mysql通配符使用: w3cchool 在mysql查询中,经常会用到通配符,而且mysql的通配符和pgsql是有所不同的,甚至mysql中还可以使用正则表达式.本文就为大家带来mysql查询中通 ...

  8. Nginx详解二十一:Nginx深度学习篇之配置苹果要求的openssl后台HTTPS服务

    配置苹果要求的证书: 1.服务器所有的连接使用TLS1.2以上的版本(openssl 1.0.2) 2.HTTPS证书必须使用SHA256以上哈希算法签名 3.HTTPS证书必须使用RSA2048位或 ...

  9. ajax beforeSend 写的显示隐藏代码不执行

    ajax如果要写像下方格式 $.ajax({ url: ajaxurl, type: 'POST', dataType: 'json', async:true, data: { }, beforeSe ...

  10. Git 将本地库添加到远程仓库

    git remote add origin ssh://admin@127.0.0.1:29418/Prjs/prj1.git git push -u origin master