import java.util.*;
public class MatchingParentheses{
public static void main(String[] args){
String str = "((())())";
boolean b = isMatch(str.toCharArray());
System.out.println(b);
}
static boolean isMatch(char[] chars){
Stack<Character> stack = new Stack();
for (char c:chars) {
if(c == '(' || c == '[' || c == '{'){
stack.push(c);
} else {
if (stack.isEmpty()) {
return false;
} else {
char top = stack.peek();
if ((top == '(' && c == ')') || (top == '[' && c == ']') || (top == '{' && c == '}')) {
stack.pop();
} else {
stack.push(c);
}
}
}
}
if (stack.isEmpty()) {
return true;
}
return false;
}
}
括号匹配 栈
最新推荐文章于 2022-10-08 15:02:55 发布