递归解决回文判断
思路,主体方法调用递归,比较首位是否相同,相同则消再递归掉比较,代码逻辑清晰,待优化。
public static void receiveString(String s) { if (s.length() == 1 || s.length() == 0) { System.out.println("yes"); return; } else if (isOK(s)) { receiveString(toShort(s)); } else { System.out.println("no"); } } private static boolean isOK(String s) { char[] ch = s.toCharArray(); return ch[0] == ch[ch.length - 1]; } private static String toShort(String s) { char[] ch = s.toCharArray(); char[] ch2 = new char[ch.length - 2]; int i = 0; for (int j = 1; j < ch.length - 1; j++) { ch2[i] = ch[j]; i++; } return new String(ch2); }