一.括号匹配问题
二.代码展示
三.总结
一.括号匹配问题
所谓括号匹配可以认为是任意的一个字符串中,都能发现一对完整的左括号与右括号按照正常的顺序实现完整匹配的过程。
不匹配的三种情况:
1.到来的右括弧非是所“期待” 的,即出现的右括号与栈中最后一个左括号不匹配。
2.到来的是“不速之客”,即再出现右括号时栈已经为空。
3.直到结束,也没有到来所“期待” 的,所有字符扫描结束后栈中仍有元素。
解决方法
遇到左括号:直接push到栈中
遇到右括号:取栈顶元素,若是对应的左括号,则匹配成功,将栈顶元素出栈,继续判断下一个元素
如果不匹配或者栈为空,则匹配不成功
扫描完成后,如果栈恰好为空,则字符串匹配,否则,字符串不匹配。
二.代码展示
package dataStructure.list;
/**
* @author Donghao Xu
*/
public class StackTest {
/*
@param paramString the given string
@return match or not.
*/
public static boolean bracketMatching(String paramString) {
//step1. initialize the stack with the element '#‘in the bottom
CharStack tempStack = new CharStack();
tempStack.push('#');
char tempChar, tempPopedChar;
//step2. put the element one by one and justify the bracket belongs to the left or the
//right
for (int i = 0; i < paramString.length(); i++) {
tempChar = paramString.charAt(i);
switch (tempChar) {
case '(':
case '[':
case '{':
tempStack.push(tempChar);
break;
case ')':
tempPopedChar = tempStack.pop();
if (tempPopedChar != '(') {
return false;
}//of if
break;
case ']':
tempPopedChar = tempStack.pop();
if (tempPopedChar != '[') {
return false;
}//of if
break;
case '}':
tempPopedChar = tempStack.pop();
if (tempPopedChar != '{') {
return false;
}//of if
break;
}//of switch
}//of for
tempPopedChar = tempStack.pop();
if (tempPopedChar != '#') {
return false;
}//of if
return true;
}//of method bracketMatching
/*
the entrance of the program
@param @args not used now.
*/
public static void main(String[] args) {
CharStack tempStack=new CharStack();
boolean tempMatch;
String tempExpression="(()";
tempMatch=bracketMatching(tempExpression);
System.out.println("Is the expression"+tempExpression+"bracket matching?"+tempMatch);
tempExpression="#(1+2)*3{[(])}";
tempMatch=bracketMatching(tempExpression);
System.out.println("Is the expression"+tempExpression+"bracket matching?"+tempMatch);
tempExpression="]{({}{}";
tempMatch=bracketMatching(tempExpression);
System.out.println("Is the expression"+tempExpression+"bracket matching?"+tempMatch);
tempExpression="@122333";
tempMatch=bracketMatching(tempExpression);
System.out.println("Is the expression"+tempExpression+"bracket matching?"+tempMatch);
tempExpression="#(1+2)*3{[()]}";
tempMatch=bracketMatching(tempExpression);
System.out.println("Is the expression"+tempExpression+"bracket matching?"+tempMatch);
tempExpression=")(";
tempMatch=bracketMatching(tempExpression);
System.out.println("Is the expression"+tempExpression+"bracket matching?"+tempMatch);
tempExpression = "( ) )";
tempMatch = bracketMatching(tempExpression);
System.out.println("Is the expression " + tempExpression + " bracket matching? " + tempMatch);
tempExpression = "()()(())";
tempMatch = bracketMatching(tempExpression);
System.out.println("Is the expression " + tempExpression + " bracket matching? " + tempMatch);
tempExpression = "({}[])";
tempMatch = bracketMatching(tempExpression);
System.out.println("Is the expression " + tempExpression + " bracket matching? " + tempMatch);
tempExpression = ")(";
tempMatch = bracketMatching(tempExpression);
System.out.println("Is the expression " + tempExpression + " bracket matching? " + tempMatch);
}
}
运行结果
Is the expression(()bracket matching?false
Is the expression#(1+2)*3{[(])}bracket matching?false
Is the expression]{({}{}bracket matching?false
Is the expression@122333bracket matching?true
Is the expression#(1+2)*3{[()]}bracket matching?true
Is the expression)(bracket matching?false
Is the expression ( ) ) bracket matching? false
Is the expression ()()(()) bracket matching? true
Is the expression ({}[]) bracket matching? true
Is the expression )( bracket matching? false
三.总结
关于符号匹配的问题,使用栈能够很容易的解决。对于匹配成功的条件(即顺序问题)栈利用后进先出的特点很好地满足了题目的要求。此外,本题中首先有一个非法字符"#‘压入栈底,来实现栈空的判断条件,是之前没有遇到过的。