问题描述:
Determine whether an integer is a palindrome. Do this without extra space.
求解一个数是不是回文数。
问题求解思路:
1.负数不是回文数
2.回文数正着读和反着读一样,故可将该数反转与原数比较大小
我的代码如下:
class Solution {
public:
bool isPalindrome(int x) {
if(x < 0) return false;
long rev = 0;
long temp = x;
while(x != 0){
rev = rev * 10 + x % 10;
x = x / 10;
}
if(rev == temp) return true;
return false;
}
};
题目比较简单,有疑问的欢迎交流~~