Test subject:
Recursive way to determine whether a string is a palindrome (palindrome) "palindrome" is the correct reading, the opposite of reading all the same sentence. For example, "I am who I was."
Design ideas:
First, the palindromic sequence is not determined that the symmetrical position of each character is the same, if a different pair of children, it is not palindromic sequence
Second, each index of the child and symmetrical position of the array is equal to the total length-1, can take each of the child data in accordance with this feature
Third, through the array need only take half the contents of the array to obtain all data
Fourth, we can start from n recursively (length) by an index, each taking the index n-1 and as a length-n performs data comparison, n is decremented by 1 each recursive
Fifth, if the number of array elements is odd, then, if the comparison to the last character, is palindromic sequence, if it is even, then the ratio of the remaining characters 0, is palindromic sequence
Source as follows:
1 public class test { 2 3 public static boolean isTrue(String str,int n) 4 { 5 int length=str.length(); 6 if(n==0||n==1) 7 return true; 8 else 9 { 10 if(str.charAt(n-1)==str.charAt(length-n)) 11 { 12 return isTrue(str,--n); 13 } 14 else 15 return to false ; 16 } . 17 } 18 is . 19 public static void main (String [] args) { 20 is Scanner Scanner = new new Scanner (the System.in); 21 is String String = Scanner.next (); 22 is IF (isTrue (String, String .length ())) 23 is System.out.println ( "palindromic sequence" ); 24 the else 25 System.out.println ( "not palindromic sequence" ); 26 is 27 } 28 }
Test Results:
Analyzed:
Should first select the appropriate algorithm before programming, if the data is too large, you should not recursion, a small amount of data can be recursively, a recursive algorithm is more relevant to people's thinking, and the code is easy to understand short, but its footprint is large, in need the right time to use.
And be entitled (structural) analysis in the hands before programming is necessary, first of all to outline the large shelf, with the overall framework go stepwise refinement can be more effective.