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. 使用应用链接来连接 Jira 和 Confluence 6

    请参考 Linking to Another Application 页面中的内容来设置如何让 Confluence 连接到你的 Jira 应用,这个过程只需要一次就可以了. 如果你计划使用 Jira ...

  2. PHP 命名空间与自动加载机制

    include 和 require 是PHP中引入文件的两个基本方法.在小规模开发中直接使用 include 和 require 没哟什么不妥,但在大型项目中会造成大量的 include 和 requ ...

  3. lightoj1336 约数和

    /*sigma(n)的每一项都可以看成是个pi的[0,ei]等比数列求和公式,那么sigma(n)就是n所有正约数的和要求找到sigma(x)为奇数的个数1<=x<=n */ 看题解的.. ...

  4. 20165314 2016-2017-2 《Java程序设计》第9周学习总结

    20165314 2016-2017-2 <Java程序设计>第9周学习总结 教材学习内容总结 URl类 UDP数据报 广播数据报 套接字 套接字连接机制 代码托管

  5. go-web项目性能测试,CPU, 内存泄露等

    go中提供了pprof包来做代码的性能监控,在两个地方有包: net/http/pprof runtime/pprof 其实net/http/pprof中只是使用runtime/pprof包来进行封装 ...

  6. 理解并设计rest/restful风格接口

    网络应用程序,分为前端和后端两个部分.当前的发展趋势,就是前端设备层出不穷(手机.平板.桌面电脑.其他专用设备......). 因此,必须有一种统一的机制,方便不同的前端设备与后端进行通信.这导致AP ...

  7. 51 Nod 1256 乘法逆元(数论:拓展欧几里得)

    1256 乘法逆元  基准时间限制:1 秒 空间限制:131072 KB 分值: 0 难度:基础题  收藏  关注 给出2个数M和N(M < N),且M与N互质,找出一个数K满足0 < K ...

  8. 【C++ Primer | 15】访问控制与继承、继承中的类作用域

    1. 只有D继承B的方式是public时,用户代码才能使用派生类向基类的转换:如果D继承B的方式是受保护的或者私有的,则用户代码不能使用该转换. 2. 不论D以什么方式继承B,D的成员函数和友员函数都 ...

  9. MyBatis-Plus工具快速入门

    MyBatis-Plus官方文档:http://mp.baomidou.com/#/quick-starthttp://mp.baomidou.com/guide/#%E7%89%B9%E6%80%A ...

  10. CentOS下配置SFTP操作日志

    1.修改ssh的配置 vi /etc/ssh/sshd_config 在36行左右修改如下配置 Subsystem sftp /usr/libexec/openssh/sftp-server -l I ...