有效的括号 给定一个只包括 '(',')','{','}','[',']' 的字符串,判断字符串是否有效。html
有效字符串需知足:sql
左括号必须用相同类型的右括号闭合。 左括号必须以正确的顺序闭合。 注意空字符串可被认为是有效字符串。bash
示例 1:ui
输入: "()" 输出: true 示例 2:spa
输入: "()[]{}" 输出: true 示例 3:netty
输入: "(]" 输出: false 示例 4:code
输入: "([)]" 输出: false 示例 5:cdn
输入: "{[]}" 输出: true 。htm
class Solution {
public boolean isValid(String str){
char[] chars = str.toCharArray();
Stack stack = new Stack();
for(char c : chars){
boolean isLeft = isLeft(c);
if(isLeft){
stack.add(c);
}else{
if (!stack.isEmpty()){
char popChar = (char)stack.pop();
if((')' == c && '(' == popChar)
|| (']' == c && '[' == popChar)
|| ('}' == c && '{' == popChar)){
continue;
}else{
return false;
}
}else{
return false;
}
}
}
if (!stack.isEmpty()){
return false;
}
return true;
}
private boolean isLeft(char c){
if('(' == c
|| '[' == c
|| '{' == c){
return true;
}
return false;
}
}
复制代码
Getting Started Metrics: 主要讲解metrics的几种类型以及metrics的demoblog
幸存者误差
在Java中12个常见的语法糖 : link.