Determine whether an integer is a palindrome. Do this without extra space.
Personal tips:利用c++新特性to_string函数转换成string然后首尾比较,代码如下:
class Solution {
public:
bool isPalindrome(int x) {
if (x < 0) return false;
string str = to_string(x);
int i = 0, j = str.size() - 1;
while (str[i]==str[j]&&i<=j)
{
++i; --j;
}
if (i >= j) return true;
else return false;
}
};
整数回文判断
本文介绍了一种不使用额外空间判断整数是否为回文数的方法。通过将整数转换为字符串,利用C++的新特性to_string函数,并采用双指针技术从两端向中间比较字符来实现。

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



