问题描述
Determine whether an integer is a palindrome. Do this without extra space.
分析
从问题描述奇少看出,代码也不会太多。关键点是不使用额外空间的情况下怎么取得对应位的值:除整取余数。如12358,取千位数的数字=>12358/1000%10.
代码
class Solution {
public:
bool isPalindrome(int x) {
long long k = x;
if (k < 0) return false;
int i = 9;
while (!((int)(x / pow(10, i)) % 10)) i--;
for (int j = i; j > i / 2; j--){
if ((int)(x / pow(10, j)) % 10 != (int)(x / pow(10, i - j)) % 10) return false;
}
return true;
}
};