版权声明:文章都是原创,转载请注明~~~~ https://blog.csdn.net/SourDumplings/article/details/86529428
Determine whether an integer is a palindrome. Do this without extra space.
Some hints:
Could negative integers be palindromes? (ie, -1)
If you are thinking of converting the integer to string, note the restriction of using extra space.
You could also try reversing an integer. However, if you have solved the problem "Reverse Integer", you know that the reversed integer might overflow. How would you handle such case?
There is a more generic way of solving this problem.
C++:
/*
@Date : 2018-01-26 09:29:27
@Author : 酸饺子 ([email protected])
@Link : https://github.com/SourDumplings
@Version : $Id$
*/
/*
https://leetcode.com/problems/palindrome-number/description/
*/
class Solution
{
public:
bool isPalindrome(int x)
{
if (x < 0)
return false;
char temp_s[100];
sprintf(temp_s, "%d", x);
int l = strlen(temp_s);
for (int i = 0, j = l - 1; i <= j; ++i, --j)
{
if (temp_s[i] != temp_s[j])
return false;
}
return true;
}
};