Thought:
Pick a base number which is the biggest 10^n smaller than x ->
Compare the Most Significant Bit and the Least Significant Bit ->
Remove the MSB and the LSB from x ->
Update the base number ->
Result
Code:
public class Solution {
public boolean isPalindrome(int x) {
if( x < 0 ) return false;
int base = 1;
int temp = x;
while( temp/10 != 0 ){
base = base * 10;
temp = temp / 10;
}
while( base > 1 ){
int MSB = x / base;
int LSB = x % 10;
if( MSB != LSB ) return false;
x = x % base;
x = x / 10;
base = base / 100;
}
return true;
}
}
No comments:
Post a Comment