public static void main(String[] args) {
System.out.println(methodC("abccbazx"));
System.out.println(methodC("abccbaz"));
System.out.println(methodD("abccbazx",0));
System.out.println(methodD("abccbaz",0));
}
//方法A,判断一个字符串是否对称
private static boolean methodA(String str) {
if (str == null)
return false;
StringBuffer buffer = new StringBuffer(str);
return str.equals(buffer.reverse().toString());
}
//方法B,左旋转一个字符一次
private static String methodB(String str) {
if (str == null)
return null;
StringBuffer buffer = new StringBuffer(str);
int index = 0;
while (index < str.length()) {
if(index == str.length()-1){
buffer.setCharAt(index, str.charAt(0));
break;
}
buffer.setCharAt(index, str.charAt(index + 1));
index++;
}
System.out.println("loop:"+buffer.toString());
return buffer.toString();
}
//方法C,用遍历的方式
private static boolean methodC(String str) {
int i = 0;
boolean flag = false;
String temp = str;
do {
flag = methodA(temp);
if (flag)
return flag;
temp = methodB(temp);
i++;
} while (i < str.length());
return flag;
}
//方法D,用递归的方式
private static boolean methodD(String str,int count) {
boolean flag = false;
flag = methodA(str);
if (flag || count == str.length()-1)
return flag;
count++;
return methodD(methodB(str),count);
}
原文地址:http://blog.csdn.net/beakin2000/article/details/45094773