错误1:
sprintf打印一个整数时会造成前导零的问题
class Solution {
public:
int getlen(int x){
int fx = abs(x);
return fx == 0 ? 1 : (((int)log10(fx)) + 1);
}
bool isPalindrome(int x) {
if(x < 0){
return false;
}
int len = x == 0 ? 1 : (((int)log10(x)) + 1);
if(len == 1){
return true;
}
int lsum = 0, rsum = 0;
int halfbitlen = (len & 1) ? ((len - 1)>>1):(len >> 1);
int rbase = (int)pow(10, halfbitlen);
int rightpart = x % rbase;
rbase *= (len & 1) ? (10) : 1;
int leftpart = x / rbase;
char numstrleft[100], numstrright[100];
// int halfDigitLen = max(getlen(rightpart),getlen(leftpart));
int halfbase = 10;
for(int i = halfbitlen - 1; i >= 0; --i){
numstrleft[i] = rightpart % halfbase;
numstrright[i] = leftpart % halfbase;
rightpart /= halfbase;
leftpart /= halfbase;
}
for(int i = 0; i < halfbitlen; ++i){
if(numstrleft[i] != numstrright[halfbitlen - 1 - i]){
return false;
}
}
return true;
}
};