描述:
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.
这里的 Some hints 其实就是想告诉你 负数不是回文数
博主这里是把int类型转化为字符串判断了
代码如下:
class Solution {
public:
bool isPalindrome(int x)
{
if (x < 0)return false;
string s = to_string(x);
for (int i = 0; i < s.length(); i++)
if (s[i] != s[s.length() - i - 1])
return false;
return true;
}
};