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;
}
};