import java.util.Stack;
public class blanket {
public static boolean isGoodBracket(String s){
Stack<Character> stack=new Stack<Character>();
for(int i=0;i<s.length();i++){
char c=s.charAt(i);
if(c=='('){
stack.push(')');
}
if(c=='['){
stack.push(']');
}
if(c=='{'){
stack.push('}');
}
if(c==')'||c==']'||c=='}'){
if(stack.size()==0)
return false;
if(stack.pop()!=c){
return false;
}
}}
if(stack.size()!=0){
return false;
}
return true;
}
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println(isGoodBracket("...(..[.)..].{.(..).}..."));
System.out.println(isGoodBracket("...(..[...].(.).).{.(..).}..."));
System.out.println(isGoodBracket(".....[...].(.).){.(..).}..."));
System.out.println(isGoodBracket("...(..[...].(.).){.(..)...."));
}
}
括号匹配问题
最新推荐文章于 2024-03-03 17:06:53 发布