Determine whether an integer is a palindrome. Do this without extra space.
#include
#include
#include
using namespace std;
class Solution {
public:
bool isPalindrome(int x) {
int num=1;//位数
int tenMax=1;//改成1 而不是10,这样num++ 记录位数就对了,如果10,num就少了一次
int i=1;
if(x<0)
return false;
/* 回文数要是自然数, 大于等于0,所以不用考虑负数了
if(x<0)
x=-x;
if(x<0)
return false;// 当 x=INT_MIN 的时候 x==-x ,所以15行的设置无用,符号的作用是取反加1,INT_MAX 即 0x8fffffff 取反加1 还是0x8fffffff
*/
while((x/10)>=tenMax)//先缩小10倍 ,防止后面tenMax溢出
{
tenMax=tenMax*10;//要考虑溢出!!!!!
num++;
}
if(num==1)
return true;
//tenMax=tenMax/10;----前面 x/10 缩小了10,所以这句不用了
while(i
注意溢出