描述:
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;
}
};
本文介绍了一种不使用额外空间判断整数是否为回文数的方法,并提供了一个C++实现的例子。文中强调了负数不被视为回文数,并讨论了将整数转换为字符串进行比较的解决方案。
8312

被折叠的 条评论
为什么被折叠?



