import java.util.Arrays;
public class Main {
public static boolean isPalindrome(char[] str, int strLen) {
if (strLen < 2) {
return true;
}
if (str[0] == str[strLen - 1]) {
return isPalindrome(Arrays.copyOfRange(str, 1, strLen - 1), strLen - 2);
} else {
return false;
}
}
public static boolean isPalindromeLoop(char[] str, int strLen) {
boolean ret = true;
for (int i = 0; i < strLen / 2; ++i) {
if (str[i] != str[strLen - i - 1]) {
ret = false;
break;
}
}
return ret;
}
public static void printPalindrome(char[] str, int strLen) {
System.out.println("Recurse:");
if (isPalindrome(str, strLen)) {
System.out.println(str + " 是回文");
} else {
System.out.println(str + " 不是回文");
}
System.out.println("Loop:");
if (isPalindromeLoop(str, strLen)) {
System.out.println(str + " 是回文");
} else {
System.out.println(str + " 不是回文");
}
}
public static void main(String[] args) {
printPalindrome("abc".toCharArray(), 3);
printPalindrome("aba".toCharArray(), 3);
printPalindrome("cccbccc".toCharArray(), 7);
}
}