leetcode 9 给一个int类型的数字,判断是否是回文数
2016-04-19 09:09
477 查看
leetcode 9 给一个int类型的数字,判断是否是回文数
题目要求:Determine whether an integer is a palindrome. Do this without extra space.
Could negative integers be palindromes? (ie, -1) 负数返回false
If you are thinking of converting the integer to string, note the restriction of using extra space.不允许额外申请空间
You could also try reversing an integer. However, if you have solved the problem "Reverse Integer", you know that the reversed integer might overflow. How would you handle such case? 当使用了反转一个int类型的数据时,考虑会不会溢出
代码:
因为不可以申请新的空间,也最好不要反转int数字,那么考虑将x每次除以10得到余数,和x/(len-1)得到首位数进行比较
bool isPalindrome(int x)
{
if(x<0)//复数返回fasle
return false;
int len=1;
int i=x;
//求出来x数字的位数
while(i/10>0)
{
i=i/10;
len++;
}
int tem1,tem2;
while(x>0)
{
tem1=x%10;
tem2=x/pow(10,(double)len-1);//10的n次方,函数pow(double x,double y)
if(tem1!=tem2)
return false;
else
{
x=x-tem1*pow(10,(double)len-1);
x=x/10;
len=len-2;
}
}
return true;
}
相关文章推荐
- 20160419运行apache2.4 丢失VCRUNTIME140.DLL的解决办法
- XAudio2学习之音频特效库XAPOFX
- gcdDepth
- HDU 1253 胜利大逃亡 (BFS)
- 图像处理与机械视觉资源(整理详细,再也不用乱找了)
- Kinect for Unity V2 代码示例(二)
- mysql存储过程语法及实例
- [置顶] [汇编语言学习笔记][第三章寄存器(内存访问)]
- linux系统免密码登陆
- [汇编语言学习笔记][第三章寄存器(内存访问)]
- codeforces 425A Sereja and Swaps
- Kinect for Unity V2 代码示例(一)
- 关于surfaceview的使用
- 引用js或css后加?v= 版本号的用法
- BZOJ 4538: [Hnoi2016]网络
- 停止在做的16件事
- Apple Swift编程语言入门教程
- SE Springer小组之《Spring音乐播放器》需求分析说明书一
- Win10 Edge浏览器更新:支持VP9等更多在线视频格式
- 定制化UINavigationBar