题目:
Determine whether an integer is a palindrome. Do this without extra space.
解答:
首先知道这个数字一共多少位,然后进行高低位逐位比较,同时注意负数肯定不是,好吧 直到今天我才知道csdn可以直接插入代码
class Solution {
public:
bool isPalindrome(int x) {
if (x < 0) return false;
if (x < 10) return true;
int high = 0, low = 0, weight = 1;
while (x / weight > 9)
weight *= 10;
while (x > 0)
{
high = x / weight;
low = x % 10;
if (low != high)
return false;
x %= weight;
x /= 10;
weight /= 100;
}
return true;
}
};