Palindrome Number
Determine whether an integer is a palindrome. Do this without extra space.
解题思路:由于不能有额外的空间,所以可以参考上面的第7题倒转数字,根据回文的性质,倒转后的数字=原数字,注意倒转过程中不要使用额外的空间(除了必要的res)。
代码如下:
class Solution {
public:
bool isPalindrome(int x) {
if(x<0) return false;
int res=0,temp=x;
while(temp>0){
res=res*10+temp%10;
temp=temp/10;
}
if(res==x){
return true;
}else{
return false;
}
}
};