Palindrome Number
2015-09-11 09:13
232 查看
Determine whether an integer is a palindrome. Do this without extra space.
Some hints:
Could negative integers be palindromes? (ie, -1)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?
bool isPalindrome(int x) {
if(x<0||(x!=0&&x%10==0))
return false;
int result=0;
while(x>result){
result=result*10+x%10;
x/=10;
}
return result==x||x==result/10;
}
Some hints:
Could negative integers be palindromes? (ie, -1)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?
bool isPalindrome(int x) {
if(x<0||(x!=0&&x%10==0))
return false;
int result=0;
while(x>result){
result=result*10+x%10;
x/=10;
}
return result==x||x==result/10;
}
相关文章推荐
- 面向对象基础知识02
- 卡片view
- 第二周项目(3):程序的多文件组织(2)
- 用javascript实现java中的集合list(后续不断完善优化)
- 利用Android的SDK工具来做应用程序测试
- Mac 下远程连接Linux
- 用javascript实现java中的集合list(后续不断完善优化)
- composer的使用总结
- halcon学习之tuple按位运算
- 如何在STM32F10x_StdPeriph_Template基础上建立自己的MDK工程(二)
- KVC实现原理简介
- Heroku上部署django
- 工作周报059
- TweenLite属性——.selector
- 【第五篇】Volley代码修改之图片二级缓存以及相关源码阅读(重写ImageLoader.ImageCache)
- 第二周项目 - 程序的多文件组织
- 第2周项目2 - 程序的多文件组织
- 昨天修复的几个编译问题
- 侧滑实现
- 注明的div+img 的3px bug