java中利用栈的知识。
package homework;
import java.util.Stack;
public class Kuohao {
/**
* 括号匹配
*/
public static boolean isGoodBracket(String s)
{
Stack<Character> a = new Stack<Character>();
for(int i=0; i<s.length(); i++)
{
char c = s.charAt(i);
if(c=='(') a.push(')');//返回的是个逆向的内容
if(c=='[') a.push(']');
if(c=='{') a.push('}');
if(c==')' || c==']' || c=='}')
{
if(a.isEmpty()) return false; // 填空
if(a.pop() != c) return false;//移除栈顶对象并作为此函数的值返回该对象
}
}
if(a.isEmpty()==false) 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("...(..[...].(.).){.(..)...."));
System.out.println( isGoodBracket("()([])"));
}
}