package com.zmz.algorithm.stackQueue;
import com.sun.org.apache.xpath.internal.operations.Bool;
import java.util.Stack;
/**
* @author 张明泽
* Create by 2022/5/22 22:34
* 有效的括号
* 阿里面试题
* LeetCode-10
*/
public class EffectiveBrackets {
public static void main(String[] args) {
String str = "[";
Boolean flag = judgeBrackets(str);
System.out.println(flag);
}
public static Boolean judgeBrackets(String str) {
Stack<Character> stack = new Stack<>();
for (char c : str.toCharArray()) {
if (c == '(') {
stack.push(')');
} else if (c == '[') {
stack.push(']');
} else if (c == '{') {
stack.push('}');
} else if (stack.isEmpty() || stack.pop() != c) return false;
}
return stack.isEmpty();
}
}
最后修改:2022 年 06 月 02 日 10 : 21 AM
© 允许规范转载