回文数:即正反都是自己
如121 1331 1 2
public class PalindromeT {
public static void main(String[] args) {
// String value = "121";
String value = "1221";
System.out.println(hasP(value));
System.out.println(hasP2(value));
}
/**
* 反转判断
*
* @param value
* @return
*/
public static boolean hasP(String value) {
if (value == null || "".equals(value)) {
return false;
}
return new StringBuilder(value).reverse().toString().equals(value);
}
/**
* 类似双向指针方式
*
* @param value
* @return
*/
public static boolean hasP2(String value) {
if (value == null || "".equals(value)) {
return false;
}
int left = 0;
int right = value.length() / 2 + 1;
while (left < value.length() / 2) {
String left1 = String.valueOf(value.charAt(left));
String right1 = String.valueOf(value.charAt(right));
if (!left1.equals(right1)) {
return false;
}
left++;
right--;
}
return true;
}
}