Determine whether an integer is a palindrome. Do this without extra space.
Could negative integers be palindromes? (ie, -1)
If you are thinking of converting the integer to string, note the restriction of using extra space.
如果是負數,則不是迴文數;如果是零,是迴文數,那大於零怎麼判斷呢?
我思考,如果我們可以把數正着寫和逆着寫的數字是一樣,那這個數就是迴文數。
下面見代碼。
class Solution {
public:
bool isPalindrome(int x) {
if(x < 0)
return false;
else if(x == 0)
return true;
else
{
int z = x;
int y = 0;
while(x != 0)
{
y = y*10 +x%10;
x = x/10;
}
if(y == z)
return true;
else
return false;
}
}
};