No.020:Valid Parentheses
问题:
Given a string containing just the characters '(', ')', '{', '}', '[' and ']',
determine if the input string is valid.
The brackets must close in the correct order, "()" and "()[]{}" are all valid but "(]" and "([)]" are not.
官方难度:
Easy
翻译:
给定一个字符串,仅包含字符:‘(’,‘)’,‘[’,‘]’,‘{’,‘}’。判断给定的字符串是不是一个合理的括号形式。括号必须顺序正确,诸如:“()”和“()[]{}”都是合理的,但是“(]”和“([)]”是不合理的。
- 首先判断字符串长度为0的特殊情况,返回true。
- 奇数长度的字符串,直接返回false。
- 定义2个字典方法,一个确定括号的方向,一个确定括号的级别。
- 这里特殊提一下,最初我认为类似“({})”不是合理的形式,但是提交时发现题目是允许这种形式的。
- 开始遍历字符串,维护一个左括号的集合。当遍历到左括号时,将这个字符加入集合;遍历到右括号时,首先判断左括号集合大小是否为0,若是,则返回false。然后比对左括号集合的最后一个元素与当前字符的括号级别和方向。
- 循环结束之后,若左括号集合大小不为0,返回false。
- 最后注意入参检查。
解题代码:
public static boolean isValid(String str) {
if (str == null) {
throw new IllegalArgumentException("Input error");
}
// 0长度特殊处理
if (str.length() == 0) {
return true;
}
// 长度为奇数可以直接判断false
if (str.length() % 2 == 1) {
return false;
}
char[] array = str.toCharArray();
// 维护左括号的集合
List<Character> leftList = new ArrayList<>();
for (int i = 0; i < array.length; i++) {
if (getDirection(array[i]) == 1) {
// 左括号处理
leftList.add(array[i]);
} else if (getDirection(array[i]) == -1) {
// 右括号处理
if (leftList.size() == 0) {
return false;
} else {
// 题目认为"({})"也是合理的形式
Character last = leftList.get(leftList.size() - 1);
if (getDirection(last) == -getDirection(array[i]) && getLevel(last) == getLevel(array[i])) {
leftList.remove(leftList.size() - 1);
} else {
return false;
}
}
} else {
throw new IllegalArgumentException("Input error");
}
}
// 循环结束之后判断leftList是否还有剩余
if (leftList.size() != 0) {
return false;
}
return true;
}
// 括号级别
private static int getLevel(Character c) {
switch (c) {
case '{':
case '}':
return 3;
case '[':
case ']':
return 2;
case '(':
case ')':
return 1;
default:
return 0;
}
}
// 括号方向
private static int getDirection(Character c) {
switch (c) {
case '{':
case '[':
case '(':
return 1;
case '}':
case ']':
case ')':
return -1;
default:
return 0;
}
}
isValid
相关链接:
https://leetcode.com/problems/valid-parentheses/
PS:如有不正确或提高效率的方法,欢迎留言,谢谢!
No.020:Valid Parentheses的更多相关文章
- LeetCode之“动态规划”:Valid Parentheses && Longest Valid Parentheses
1. Valid Parentheses 题目链接 题目要求: Given a string containing just the characters '(', ')', '{', '}', '[ ...
- LeetCode OJ:Valid Parentheses(有效括号)
Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the inpu ...
- LeetCode专题-Python实现之第20题:Valid Parentheses
导航页-LeetCode专题-Python实现 相关代码已经上传到github:https://github.com/exploitht/leetcode-python 文中代码为了不动官网提供的初始 ...
- LeetCode第[20]题(Java):Valid Parentheses
题目:有效的括号序列 难度:Easy 题目内容: Given a string containing just the characters '(', ')', '{', '}', '[' and ' ...
- [LeetCode 题解]: Valid Parentheses
Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the inpu ...
- leetcode题解:Valid Parentheses(栈的应用-括号匹配)
题目: Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the ...
- leetcode解题报告(7):Valid Parentheses
描述 Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the i ...
- leetcode:Valid Parentheses
括号匹配 Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the ...
- LeetCode 020 Valid Parentheses
题目描述:Valid Parentheses Given a string containing just the characters '(', ')', '{', '}', '[' and ']' ...
随机推荐
- Atitit 从 RGB 到 HSL 或 HSV 的转换
Atitit 从 RGB 到 HSL 或 HSV 的转换 1.1. 从 RGB 到 HSL 或 HSV 的转换公式与原理1 1.2. public static HSV RGB2HSV(Color ...
- PHP实现RESTful风格的API实例(二)
接前一篇PHP实现RESTful风格的API实例(一) Response.php :包含一个Request类,即输出类.根据接收到的Content-Type,将Request类返回的数组拼接成对应的格 ...
- Java override 和 overload 的区别
override 是重写(覆盖)了一个方法,用来实现不同的功能,一般是用于子类继承父类时,重写父类的方法的时候. 重写(覆盖)的规则: 1.重写方法的参数列表必须表示与被重写的方法相同,否则不能称为重 ...
- Asp.Net实现无刷新文件上传并显示进度条(非服务器控件实现)(转)
Asp.Net实现无刷新文件上传并显示进度条(非服务器控件实现) 相信通过Asp.Net的服务器控件上传文件在简单不过了,通过AjaxToolkit控件实现上传进度也不是什么难事,为什么还要自己辛辛苦 ...
- Java EE开发平台随手记1
过完春节以来,一直在负责搭建公司的新Java EE开发平台,所谓新平台,其实并不是什么新技术,不过是将目前业界较为流行的框架整合在一起,做一些简单的封装和扩展,让开发人员更加易用. 和之前负责具体的项 ...
- css知多少(5)——选择器
1. 引言 从本节开始,就进入本系列的第二个部分——css和html的结合——说白了就是选择器. CSS中定义了样式,如何将这些样式设置到相应的html节点上?就不得不通过选择器.让浏览器知道css选 ...
- MS SQL Server存储过程
1.Create.Alter和Drop CREATE PROCEDURE USP_CategoryList AS SELECT CategoryID,CategoryName FROM Categor ...
- javascript中的错误处理机制
× 目录 [1]对象 [2]类型 [3]事件[4]throw[5]try[6]常见错误 前面的话 错误处理对于web应用程序开发至关重要,不能提前预测到可能发生的错误,不能提前采取恢复策略,可能导致较 ...
- javascript运算符——关系运算符
× 目录 [1]恒等 [2]相等 [3]大于[4]小于 前面的话 关系运算符用于测试两个值之间的关系,根据关系是否存在而返回true或false,关系表达式总是返回一个布尔值,通常在if.while或 ...
- Javaweb -- ServletContextListener
当启动web应用后端服务时,有时需要预先从数据库或者配置文件等读取信息来配置一些全局变量之类的 这时可以用ServletContextListener,在启动服务时,加载设置基本配置 实现如下: (1 ...