9. Palindrome Number
2016-01-22 23:37
357 查看
/* 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 */9. Palindrome Number
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 */9. Palindrome Number
public class Solution { public boolean isPalindrome(int x) { if(x<0) return false; int answer = 0; int y = x; while (x != 0) { answer = 10 * answer + x % 10; x /= 10; } return (answer == y); } }
相关文章推荐
- codeforces132C
- 跟siki老师学C#第七天
- stl中的空间配置器
- Ace教你一步一步做Android新闻客户端(五) 优化Listview
- t持久化与集群部署开发详解
- [读书笔记]C#学习笔记七: C#4.0中微小改动-可选参数,泛型的可变性
- cvResize() 图像放缩
- 增加和删除用户组与用户
- 值得 Web 开发人员学习的20个 jQuery 实例教程
- codeforces 258B
- MAC 10.10.5编译opencv3.0(java版本)
- 三种弹窗的解析
- 蓝桥杯 日期计算
- coco2dx-js 实现 层拦截 点击响应事件
- Java集合(实现类线程安全性)
- php 获取自己的公网IP
- “请运行Chkdsk工具”怎么解决
- innerHTML和nnerText造成的乱码现象 以及 background-image放入.css中失效
- UVALive 4225 / HDU 2964 Prime Bases 贪心
- String、StringBuffer和StringBuilder类