这道题是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. jasmine+karma 自动化单元测试

    测试的必须性 相信大家都知道测试的必要性,测试先行的概念.不过,写了这么多年的代码,除了之前用java的时候写过一些测试用例,还真的很少写过前端的测试用例,或者做一些自动化测试.感觉做单元测试还是很有 ...

  2. Java 8新特性--Lambda表达式作为返回值

    lambda表达式作为方法的返回值:

  3. 你不知道的HTTP之首部字段一览

    一.HTTP/1.1 首部字段一览 HTTP 首部字段根据实际用途被分为以下 4 种类型: 1.通用首部字段:请求报文和响应报文两方都会使用的首部. 首部字段名 说明 Cache-Control 控制 ...

  4. Python 字符编码问题的处理

    python中的字符编码问题往往是初学者容易弄不明白的问题, 要想将这个问题搞清楚,需要先弄明白以下的概念 decode 和 encode 函数的作用 字符串字面量的编码格式 decode(str)  ...

  5. Ambiguous mapping. Cannot map 'registerController' method

    org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'requestMappi ...

  6. 创建一个文件夹用于写入UTF-8编码的文件

    实现效果: 知识运用: File类的CreateText方法 StreamWriter类的WriteLine方法 实现代码: private void button2_Click(object sen ...

  7. CPP-基础:字节对齐

    一.   什么是字节对齐,为什么要对齐?      现代计算机中内存空间都是按照byte划分的,从理论上讲似乎对任何类型的变量的访问可以从任何地址开始,但实际情况是在访问特定类型变量的时候经常在特定的 ...

  8. struts2的单个文件上传

    本文主要两种方式,一:通过 FileUtils.copyFile(file, savefile);方法复制:二:通过字节流方式复制 web.xml <?xml version="1.0 ...

  9. Clover启动mbr的win7/win8

    对以传统bios安装在mbr分区的win7/WIN8也可以使用EFI引导直接进入win.首先进win提取EFI引导文件,以管理员员身份运行cmd,输入如下命令 bcdboot c:\windows / ...

  10. linux配置nodeJs环境教程

    来自阿里云:https://help.aliyun.com/document_detail/50775.html