Before showing mine codes,i'd like to let you know two things:
1. It can functions on negative numbers.
2. It uses a sub-function called 'reverseInteger' to achieve the reversion of later
half of aimed number.
You can test with 0,101,1001,10001,100001,10201,1002001,11,111 and so on.
Here is the codes:
public class PalindromeNumber {
public boolean isPalindromeNumber(int x) {
boolean flag = false;
if ( x < 0 ) {
x = -x;
}
int b = ( x + "" ).length();
int half = (int)( b / 2 );
int temp = ( b % 2 == 0 )?
new Double( Math.pow( 10 , half ) ).intValue():
new Double( Math.pow( 10 , ( half + 1 ) ) ).intValue();
int a1 = (int)( x / temp );
int b1 = ( a1 + "" ).length();
int a2 = reverseInteger(half,x);
int b2 = ( a2 + "" ).length();
if ( b1 == b2 ) {
if ( a1 == a2 ) {
flag = true;
}
}
return flag;
}
public int reverseInteger(int digit,int x) {
int count = 0;
int newrvs = 0;
while( x != 0 && count < digit) {
newrvs = newrvs * 10 + x % 10;
x = (int)( x / 10 );
count++;
}
return newrvs;
}
public static void main (String [] args) {
boolean z = new PalindromeNumber().isPalindromeNumber(10);
System.out.println(z);
}
}