这道题是LeetCode里的第20道题。

题目要求:

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

有效字符串需满足:

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

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

示例 1:

输入: "()"
输出: true

示例 2:

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

示例 3:

输入: "(]"
输出: false

示例 4:

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

示例 5:

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

这还用想吗?堆栈无脑解题!!!左括号进栈,右括号判断并出栈。

题解代码:

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

提交结果:

个人总结:

啥都没有,但是得贴个 python 的解法:

class Solution:
def isValid(self, s):
while '{}' in s or '()' in s or '[]' in s:
s = s.replace('{}', '')
s = s.replace('[]', '')
s = s.replace('()', '')
return s == ''

python 真是简洁啊!

【LeetCode】Valid Parentheses(有效的括号)的更多相关文章

  1. [LeetCode] Valid Parentheses 验证括号

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

  2. [Leetcode] valid parentheses 有效括号对

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

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

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

  4. [LeetCode] Valid Parenthesis String 验证括号字符串

    Given a string containing only three types of characters: '(', ')' and '*', write a function to chec ...

  5. LeetCode: Valid Parentheses 解题报告

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

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

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

  7. LeetCode OJ:Valid Parentheses(有效括号)

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

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

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

  9. 【LeetCode】1111. Maximum Nesting Depth of Two Valid Parentheses Strings 有效括号的嵌套深度

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目讲解 划分规则讲解 返回结果讲解 解题方法 代码 日期 题目地址:ht ...

随机推荐

  1. uvm_test——测试用例的起点

    在UVM平台验证中,所有的test cases都extends uvm_test,首先,来看源代码 //------------------------------------------------ ...

  2. MySQL主服务配置文件

    [mysql]port=3306socket=/var/lib/mysql/mysql.sockdefault-character-set = utf8mb4 [mysqld]server-id = ...

  3. Windows定时任务管理以及服务管理

    1.NSSM.exe https://nssm.cc/ 2.Topshelf 引用地址:https://www.cnblogs.com/guogangj/p/10093102.html#4136330

  4. ftpclient 遇到的一些问题

    1. FTPFile[] files=ftpClient.listFiles(ftpDirectory); 没有数据 public static boolean ftpLogin(String ser ...

  5. UVA 10735 Euler Circuit (最大流)

    题意:求混合图的欧拉路径. 一句话总结:网络流,最主要在于建图,此题是将出度则是和流量联系在了一起,用最大流来调整边的指向. 分析: 这题的困难之处在于无向边只能用一次,相当于一个方向未定的有向边. ...

  6. iOS,APP退到后台,获取推送成功的内容并且语音播报内容。

    老铁,我今天忙了一下午就为解决这个问题,网上有一些方法,说了一堆关于这个挂到后台收到推送并且获得推送内容的问题,有很多人都说APP挂到后台一会就被杀死.但实际上可以有办法解决的. WechatIMG3 ...

  7. sort函数的使用

    此篇当作自己的笔记(水平太菜,这都一直没搞明白) sort()函数的用法1)sort函数包含在头文件<algroithm>中,还要结合using namespace std2)sort有三 ...

  8. Dance links算法

    其实Dance links只是一种数据结构,Dance links 才是一种算法.dacing links x就是一个高效的求解该类问题的算法,而这种算法,基于交叉十字循环双向 链表.下面是双向十字链 ...

  9. (4)JSTL的SQL标签库

    jstl的SQL标签库 SQL tag Library中的标签用来提供在 JSP 页面中可以与数据库进行交互的功能Database access标签库有以下6组标签来进行工作: <sql:set ...

  10. ios 团购信息客户端demo(一)

    团购信息客户端,主要整合了ASIHTTPREQUEST,KISSXML,AQGridView,MBProgressHUD这几个主要流行的IOS开发库,我们先来看一下效果图 首先我们新建一个IOS工程, ...