9. Palindrome Number
2016-01-19 23:26
232 查看
Determine whether an integer is a palindrome. Do this without extra space.
click to show spoilers.
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?
There is a more generic way of solving this problem.
Subscribe to see which companies asked this question
判断一个数字是否是回文,并且不需要额外的空间开销。
我的解法思路是
1窜数字如果顺着组合和倒着组合都相等,那么就认为这是回文。
即我们只要倒着组合一窜数字即可。
click to show spoilers.
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?
There is a more generic way of solving this problem.
Subscribe to see which companies asked this question
判断一个数字是否是回文,并且不需要额外的空间开销。
我的解法思路是
1窜数字如果顺着组合和倒着组合都相等,那么就认为这是回文。
即我们只要倒着组合一窜数字即可。
public boolean isPalindrome(int x) { int tmp =x; if(x<0) { return false; } else { int sum=0; //依次取出低位到高位的各个数字。 while( x != 0) { sum *=10; sum += x%10; x = x/10; } //组合的数字和原来的相等,即认为是回文 if(sum == tmp) return true; else return false; } }
相关文章推荐
- Python每日一练(1):计算文件夹内各个文章中出现次数最多的单词
- 在linux中安装nginx
- 初步了解UBOOT (4)
- jenkins 入门教程(中)
- Linux账号密码过期会导致crontab作业不能执行
- Java Swing JTextArea,JScrollPane——实现数据的刷新到最底行
- 隐藏在手机中的好玩代码
- iOS学习之数据持久化详解
- Android开发学习:ImageView的scaletype属性
- Android Studio——HttpClient
- JAVA源码学习-LinkedList
- 使用Java的JNative调用dll动态链接库
- 23333 又是一篇水文章(以下是各种复制来的关于maven转成eclipse项目)
- 2015年11月26日 Java基础系列(六)正则表达式Regex
- [转载]通达信插件选股(基于通达信插件编程规范的简单分析)
- 随笔1.19.2016
- checkbox标签已有checked=checked但是不显示勾选
- 20151227-训练题R2
- quartz应用
- quartz应用