不能用多余的空间不是不能设置变量了,只是不能用数组了。尾部的先取余后除,头部的先除后取余。
class Solution {
public:
bool isPalindrome(int x) {
if(x<0) return false;
if(x<10) return true;
int cnt=0;
int tmp=x;
while(tmp){
tmp/=10;
cnt++;
}
for(int i=0;i<cnt/2;i++){
int tp1=1,tp2=1;
for(int j=0;j<=i;j++) tp1*=10;
for(int j=0;j<cnt-i-1;j++) tp2*=10;
if(x%tp1/(tp1/10)!=x/tp2%10) return false;
}
return true;
}
};