思路:基于栈判断括号配对是否正确;
方法:左括号保存,右括号配对消除。
①读到“(”:入栈;
②读到“)”:与和栈顶的“(”配对消解,弹出栈顶“)”;
③若输入读完且栈为空:配对成功,算法停止;
若栈先空:输入剩下的“)”已无可配对的“(”,出错;
若输入先读完,栈出剩余的“(”已经没有可以配对的“)”,出错。
package linear_list;
/**
* Stack is already defined in Java.
*
* @author 前夜
*
*/
public class CharStack {
public static final int MAX_DEPTH = 10;
int depth;
char[] data;
public CharStack() {
depth = 0;
data = new char[MAX_DEPTH];
}
/**
* Push an element.
*
* @param paraChar
* @return
*/
public boolean push(char paraChar) {
if (depth == MAX_DEPTH) {
System.out.println("The Stack ids full, so you can not push element.");
return false;
} // Of if
data[depth] = paraChar;
depth++;
return true;
}// Of push
/**
* Pop an element.
*/
public char pop() {
if (depth == 0) {
System.out.println("The Stack is empty, so there is nothing to pop.");
return '\0';
} // Of if
char resultChar = data[depth - 1];
depth--;
return resultChar;
} // Of pop
public String toString() {
String resultString = "";
for (int i = 0; i < depth; i++) {
resultString += data[i];
} // Of for i
return resultString;
} // Of toString
public static boolean bracketsMatching(String paraString) {
// Firstly, initialize the stack and push a '#' at the bottom.
// 初始化字符串,并且把#放在栈底。
CharStack tempStack = new CharStack();
tempStack.push('#');
char tempChar, tempPopedChar;
// Secondly, process the string.
// 处理字符串。
for (int i = 0; i < paraString.length(); i++) {
tempChar = paraString.charAt(i); // 返回指定索引处的char值
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;
default:
} // Of switch
} // Of for
tempPopedChar = tempStack.pop();
if (tempPopedChar != '#') {
return false;
} // Of if
return true;
}// Of bracketsMatching
public static void main(String[] args) {
// TODO Auto-generated method stub
CharStack tempStack = new CharStack();
for (char ch = 'A'; ch < 'M'; ch++) {
tempStack.push(ch);
System.out.println("Now the stack is: " + tempStack);
} // Of for ch
char tempChar;
for (int i = 0; i < 12; i++) {
tempChar = tempStack.pop();
System.out.println("The poped stack is: " + tempChar);
System.out.println("Now the stack is: " + tempStack);
} // Of for i
boolean tempMatch;
String tempExpression = "[5 * 2 + 8 + (1 - 3)] * 4";
tempMatch = bracketsMatching(tempExpression);
System.out.println("Is the expression " + tempExpression + " bracket matching? " + tempMatch);
tempExpression = "( ) }";
tempMatch = bracketsMatching(tempExpression);
System.out.println("Is the expression " + tempExpression + " bracket matching? " + tempMatch);
tempExpression = "()()({})";
tempMatch = bracketsMatching(tempExpression);
System.out.println("Is the expression " + tempExpression + " bracket matching? " + tempMatch);
tempExpression = "({}[])";
tempMatch = bracketsMatching(tempExpression);
System.out.println("Is the expression " + tempExpression + " bracket matching? " + tempMatch);
tempExpression = ")(";
tempMatch = bracketsMatching(tempExpression);
System.out.println("Is the expression " + tempExpression + " bracket matching? " + tempMatch);
}// Of main
}// Of CharStack
相关思考:
①length与length()的区别:
在java中String类可以定义字符串变量和字符串数组,length()用于求String字符串对象的长度,而length用于求String字符串数组的长度。更加直白的说法就是: length()是求String字符串对象中字符的个数,而length是求字符串数组中有多少个字符串。
②charAt(i):
属于字符串类中的String类常用方法,charAt(int index) 的返回类型为char,表示返回指定索引处的char值。