【LeetCode算法-20】Valid Parentheses
LeetCode第20题
Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the input string is valid.
An input string is valid if:
- Open brackets must be closed by the same type of brackets.
- 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的更多相关文章
- [Leetcode][Python]20: Valid Parentheses
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 20: Valid Parentheseshttps://oj.leetcod ...
- 《LeetBook》leetcode题解(20):Valid Parentheses[E]——栈解决括号匹配问题
我现在在做一个叫<leetbook>的免费开源书项目,力求提供最易懂的中文思路,目前把解题思路都同步更新到gitbook上了,需要的同学可以去看看 书的地址:https://hk029.g ...
- 【算法】LeetCode算法题-Valid Parentheses
这是悦乐书的第147次更新,第149篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第6题(顺位题号是20),给定一个只包含字符'(',')','{','}','['和'] ...
- C# 写 LeetCode easy #20 Valid Parentheses
20.Valid Parentheses Given a string containing just the characters '(', ')', '{', '}', '[' and ']', ...
- 【一天一道LeetCode】#20. Valid Parentheses
一天一道LeetCode系列 (一)题目 Given a string containing just the characters '(', ')', '{', '}', '[' and ']', ...
- LeetCode题解(20)--Valid Parentheses
https://leetcode.com/problems/valid-parentheses/ 原题: Given a string containing just the characters ' ...
- 【LeetCode】20. Valid Parentheses 有效的括号
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 个人公众号:负雪明烛 本文关键词:有效,括号,括号匹配,栈,题解,leetcode, 力扣 ...
- LeetCode:20. Valid Parentheses(Easy)
1. 原题链接 https://leetcode.com/problems/valid-parentheses/description/ 2. 题目要求 给定一个字符串s,s只包含'(', ')', ...
- LeetCode算法01 Valid Parentheses
Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the inpu ...
- 【LeetCode】20. Valid Parentheses
题目:
随机推荐
- html学习——基础分类总结
1. html 超文本标记语言HyperText Markup Language.html文档基本结构: <!DOCTYPE html><head> <! ...
- 一篇文章让你了解Android各个版本的历程
2008年--至今 Android 1.5(Cupcake纸杯蛋糕): 智能虚拟键盘:使用widgets实现桌面个性化:在线文件夹(Live Folder)快速浏览在线数据:视频录制和分享:图片上传: ...
- 《JavaWeb从入门到精通》(明日科技,清华大学出版社)
<JavaWeb从入门到精通>(明日科技,清华大学出版社)
- Confluence 6 其他需要备份和恢复的地方
XML 备份被描述用于在 Confluence 备份使用的其他方法,例如升级和移动服务器.使用上面描述的备份和恢复方法也适用这些地方. 我们的 upgrade guide 不要求使用一个 XML 备份 ...
- ajax执行成功后,在success回调函数中把后台返回的list还原到html的table中
需求描述:前台通过onclick触发ajax,到后台返回一个list(json格式的),把list插入到html的table中. 思路简介: ̄□ ̄|| 刚开始的时候,是没有思路的,就卡在了,怎么把 a ...
- HighCharts基本使用
一.简叙 HighCharts是一个非常强大的画图插件,在以后的工作汇报,数字展示,它将是一把利器.既然是插件,那么就有它的使用规则,我们只需要遵循它的使用规则,就可以画出我们想要的展示效果了.期待吗 ...
- shell 排除目录
1.新建文件 exclude.txt,在文件中写需要排除的目录(只需要目录名称,不需要路径) 2.--exclude-from='/data/www/vhosts/git_track/git-shel ...
- Loadrunner11.0 录制手机App脚本的方法二
lr11安装一个移动补丁以后,就可以支持抓包文件直接转换为http/html协议的脚本.下面简单说一下过程. 1.工作思路:手机连接可上外网wifi,在手机wifi中设置本机ip的网络地址,然后在本机 ...
- 微信公众号开发调用自带地图 不显示(openLocation)
1.需要在wx.config中声明需要使用的功能(openLocation) 例如: wx.config({ debug: false, // 开启调试模式,调用的所有api的返回值会在客户端aler ...
- 20165206 2017-2018-2 《Java程序设计》第五周学习总结
20165206 2017-2018-2 <Java程序设计>第五周学习总结 教材学习内容总结 内部类:支持在一个类中定义另一个类的类. 外嵌类:包含内部类的类,称为内部类的外嵌类. 匿名 ...