/** ********************* * Is the bracket matching? * * @param paraString The given expression. * @return Match or not. ********************* */ publicstaticbooleanbracketMatching(String paraString) { // Step 1. Initialize the stack through pushing a '#' at the bottom. CharStacktempStack=newCharStack(); tempStack.push('#'); char tempChar, tempPopedChar;
// Step 2. Process the string. For a string, length() is a method // instead of a member variable. for (inti=0; i < paraString.length(); i++) { tempChar = paraString.charAt(i);
switch (tempChar) { case'(': case'[': case'{': tempStack.push(tempChar); break; case')': tempPopedChar = tempStack.pop(); if (tempPopedChar != '(') { returnfalse; } // Of if break; case']': tempPopedChar = tempStack.pop(); if (tempPopedChar != '[') { returnfalse; } // Of if break; case'}': tempPopedChar = tempStack.pop(); if (tempPopedChar != '{') { returnfalse; } // Of if break; default: // Do nothing. }// Of switch } // Of for
tempPopedChar = tempStack.pop(); if (tempPopedChar != '#') { returnfalse; } // Of if
/** * Char stack. I do not use Stack because it is already defined in Java. * * @author Shihuai Wen Email:wshysxcc@outlook.com */ publicclassCharStack { /** * The depth. */ privatestaticfinalintMAX_DEPTH=10;
/** * The actual depth. */ int depth;
/** * The data */ char[] data;
/** ********************* * Construct an empty char stack. ********************* */ publicCharStack() { depth = 0; data = newchar[MAX_DEPTH]; }// Of the first constructor
/** ********************* * Overrides the method claimed in Object, the superclass of any class. ********************* */ @Override public String toString() { StringresultString=""; for (inti=0; i < depth; i++) { resultString += data[i]; } // Of for i
return resultString; }// Of toString
/** ********************* * Push an element. * * @param paraChar The given char. * @return Success or not. ********************* */ publicbooleanpush(char paraChar) { if (depth == MAX_DEPTH) { System.out.println("Stack full."); returnfalse; } // Of if
data[depth] = paraChar; depth++;
returntrue; }// Of push
/** ********************* * Pop an element. * * @return The popped char. ********************* */ publiccharpop() { if (depth == 0) { System.out.println("Nothing to pop."); return'\0'; } // Of if
charresultChar= data[depth - 1]; depth--;
return resultChar; }// Of pop
/** ********************* * Is the bracket matching? * * @param paraString The given expression. * @return Match or not. ********************* */ publicstaticbooleanbracketMatching(String paraString) { // Step 1. Initialize the stack through pushing a '#' at the bottom. CharStacktempStack=newCharStack(); tempStack.push('#'); char tempChar, tempPopedChar;
// Step 2. Process the string. For a string, length() is a method // instead of a member variable. for (inti=0; i < paraString.length(); i++) { tempChar = paraString.charAt(i);
switch (tempChar) { case'(': case'[': case'{': tempStack.push(tempChar); break; case')': tempPopedChar = tempStack.pop(); if (tempPopedChar != '(') { returnfalse; } // Of if break; case']': tempPopedChar = tempStack.pop(); if (tempPopedChar != '[') { returnfalse; } // Of if break; case'}': tempPopedChar = tempStack.pop(); if (tempPopedChar != '{') { returnfalse; } // Of if break; default: // Do nothing. }// Of switch } // Of for
tempPopedChar = tempStack.pop(); if (tempPopedChar != '#') { returnfalse; } // Of if
returntrue; }// Of bracketMatching
/** ********************* * The entrance of the program. * * @param args Not used now. ********************* */ publicstaticvoidmain(String args[]) { CharStacktempStack=newCharStack();
for (charch='a'; ch < 'm'; ch++) { tempStack.push(ch); System.out.println("The current stack is: " + tempStack); } // Of for i
char tempChar; for (inti=0; i < 12; i++) { tempChar = tempStack.pop(); System.out.println("Poped: " + tempChar); System.out.println("The current stack is: " + tempStack); } // Of for i
/** ********************* * Sum to N. No loop, however a stack is used. * * @param paraN The given value. * @return The sum. ********************* */ publicstaticintsumToN(int paraN) { if (paraN <= 0) { // Basis. return0; } // Of if
/** ********************* * Fibonacci sequence. * * @param paraN The given value. * @return The sum. ********************* */ publicstaticintfibonacci(int paraN) { if (paraN <= 0) { // Negative values are invalid. Index 0 corresponds to the first element 0. return0; } if (paraN == 1) { // Basis. return1; } // Of if
/** ********************* * Fibonacci sequence.Tail recursion * * @param paraN The given value. * @param paraPre The previous value in fibonacci sequence. * @param paraCur The current value in fibonacci sequence. * @return The sum. ********************* */ publicstaticintfibonacci_new(int paraN, int paraPre, int paraCur) { if (paraN <= 0) { // Negative values are invalid. Index 0 corresponds to the first element 0. return0; } if (paraN == 1) { // Basis. return paraCur; } // Of if
/** * Recursion. A method can (directly or indirectly) invoke itself. The system * automatically creates a stack for it. * * @author Shihuai Wen Email:wshysxcc@outlook.com */ publicclassRecursion { /** ********************* * Sum to N. No loop, however a stack is used. * * @param paraN The given value. * @return The sum. ********************* */ publicstaticintsumToN(int paraN) { if (paraN <= 0) { // Basis. return0; } // Of if
return sumToN(paraN - 1) + paraN; }// Of sumToN
/** ********************* * Fibonacci sequence. * * @param paraN The given value. * @return The sum. ********************* */ publicstaticintfibonacci(int paraN) { if (paraN <= 0) { // Negative values are invalid. Index 0 corresponds to the first element 0. return0; } if (paraN == 1) { // Basis. return1; } // Of if
/** ********************* * Fibonacci sequence.Tail recursion * * @param paraN The given value. * @param paraPre The previous value in fibonacci sequence. * @param paraCur The current value in fibonacci sequence. * @return The sum. ********************* */ publicstaticintfibonacci_new(int paraN, int paraPre, int paraCur) { if (paraN <= 0) { // Negative values are invalid. Index 0 corresponds to the first element 0. return0; } if (paraN == 1) { // Basis. return paraCur; } // Of if
/** ********************* * The entrance of the program. * * @param args Not used now. ********************* */ publicstaticvoidmain(String args[]) { inttempValue=5; System.out.println("0 sum to " + tempValue + " = " + sumToN(tempValue)); tempValue = -1; System.out.println("0 sum to " + tempValue + " = " + sumToN(tempValue)); longstartTime= System.currentTimeMillis(); for (inti=0; i < 40; i++) { System.out.println("Fibonacci " + i + ": " + fibonacci(i)); // System.out.println("Fibonacci_new " + i + ": " + fibonacci_new(i, 0, 1)); } // Of for i longendTime= System.currentTimeMillis(); System.out.println("Execute time is: " + (endTime - startTime) + " ms"); }// Of main }// Of class Recursion
总结
无论是括号匹配还是递归代码, 一定都是先有思路然后再进行雕琢.
思考的过程不能少, 不能拿到需求就直接 main 和 print.