LeetCode第20题

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

An input string is valid if:

  1. Open brackets must be closed by the same type of brackets.
  2. Open brackets must be closed in the correct order.

Note that an empty string is also considered valid.

Example 1:

Input: "()"
Output: true

Example 2:

Input: "()[]{}"
Output: true

Example 3:

Input: "(]"
Output: false

Example 4:

Input: "([)]"
Output: false

Example 5:

Input: "{[]}"
Output: true

思路:

本来我的想法是不管(),[],{},都是在一起的,我一对一对的删掉,最后删空了,就符合要求

代码

class Solution {
public boolean isValid(String s) {
if(s.length()>Integer.MAX_VALUE){
return true;
}
for(int i = 0;i<3;i++){
for(int j = 0;j<s.length();j++){
s = s.replace("()","");
System.out.println(s);
s = s.replace("[]","");
System.out.println(s);
s = s.replace("{}","");
System.out.println(s);
}
}
if("".equals(s)){
return true;
}else{
return false;
}
}
}

结果

搞这么多符号,这不故意整我吗

百度了下,都说用栈,代码如下

class Solution {
public boolean isValid(String s) {
Stack<String> stack = new Stack<String>();
for (int i = 0; i < s.length(); i++) {
char candidate = s.charAt(i);
if (candidate == '{' || candidate == '[' || candidate == '(') {
stack.push(candidate + "");
} else {
if (stack.isEmpty()) {
return false;
}
if ((candidate == '}' && stack.peek().equals("{")) ||
(candidate == ']' && stack.peek().equals("[")) ||
(candidate == ')' && stack.peek().equals("("))) {
stack.pop();
} else {
return false;
}
}
}
if (stack.isEmpty()) {
return true;
} else {
return false;
}
}
}

就是利用后进先出的原理,其实跟我的思路差不多,但是性能要好很多,哈哈

【LeetCode算法-20】Valid Parentheses的更多相关文章

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

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

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

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

  3. 【算法】LeetCode算法题-Valid Parentheses

    这是悦乐书的第147次更新,第149篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第6题(顺位题号是20),给定一个只包含字符'(',')','{','}','['和'] ...

  4. C# 写 LeetCode easy #20 Valid Parentheses

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

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

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

  6. LeetCode题解(20)--Valid Parentheses

    https://leetcode.com/problems/valid-parentheses/ 原题: Given a string containing just the characters ' ...

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

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

  8. LeetCode:20. Valid Parentheses(Easy)

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

  9. LeetCode算法01 Valid Parentheses

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

  10. 【LeetCode】20. Valid Parentheses

    题目:

随机推荐

  1. PHP 转义

    函数名 释义 介绍 htmlspecialchars 将与.单双引号.大于和小于号化成HTML格式 &转成&"转成"' 转成'<转成<>转成> ...

  2. Oracle_plsql_开发工具搭建最小化客户端

    一:资源下载获取路径: 二:配置方法 1:前提是安装好plsql开发工具 具体安装步骤略 2:配置 简化版的客户端工具. 具体格式:可以参照下文来修改编写使用. orcl_1521 = (DESCRI ...

  3. Confluence 6 SQL Server 问题解决

    如果你收到了下面的错误信息,检查你给出的 confluenceuser 用户具有所有需要的数据库权限,当你使用 localhost 进行连接的时候. Could not successfully te ...

  4. 分布式Dubbo快速入门

    目录 Dubbo入门 背景 zookeeper安装 发布Dubbo服务 Dubbo Admin管理 消费Dubbo服务 抽取与依赖版本管理 Dubbo入门 Editor:SimpleWu Dubbo是 ...

  5. webpack2配置备份

    package.json: { "name": "leyi", "version": "1.0.0", "ma ...

  6. (批量更新)对多个符合条件的id做更新操作

    需求描述:把checkbox勾选的对应id的记录的标志位置1或0,这个其实不难的,不过我自己做的话,肯定是多次访问数据库做更新,看了老大的代码,发现差距不是一般的大,老大把sql灵活运用,结果一次访问 ...

  7. python(4): regular expression正则表达式/re库/爬虫基础

    python 获取网络数据也很方便 抓取 requests 第三方库适合做中小型网络爬虫的开发, 大型的爬虫需要用到 scrapy 框架 解析 BeautifulSoup 库, re 模块 (一) r ...

  8. python unittest装载、执行、造成报告

    #执行用例 caselist = config.caselist for i in range(0,len(caselist)): reportname = caselist[i][2:] now = ...

  9. java 数据类型相关的内容

    √基本数据默认值是:0 √ 引用类型是默认值:null 局部变量不能初始化 单精度浮点有后缀: Flong后面如果不加后缀L系统将默认为int类型 不管单精度还是双精度浮点,都比整数类型的范围大,原因 ...

  10. Ajax增删改查-----------删 改

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...