LeetCode 第 9 题(Palindrome Number)
2016-04-18 13:19
246 查看
LeetCode 第 9 题(Palindrome Number)
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?
There is a more generic way of solving this problem.
这道题很简单,可以利用第 7 题的部分代码。 第 7 题将一个整数的各个十进制位翻转了。如果翻转之后数字没有变化就说明是个 palindrome。
而且我们也不用考虑所谓的整数溢出问题,因为发生溢出的数肯定不是 palindrome。因此,就有了下面的代码。
bool isPalindrome(int x) { if(x < 0) return false; int ret = 0, xx = x; do { ret = 10 * ret + xx % 10; xx = xx / 10; }while(xx); return ret == x; }
相关文章推荐
- 第八周项目(1)-数组做数据成员(工资)
- boa服务器make错误
- MySQL并发复制系列三:MySQL和MariaDB实现对比
- mysql自动停止 Plugin FEDERATED is disabled 的完美解决方法
- HDU 2444 二分图
- java里类方法和实例方法区别
- 深入理解java异常处理机制
- _视图控制对象生命周期-init、viewDidLoad、viewWillAppear、viewDidAppear、viewWillDisappear等的区别及用途
- java集合-TreeMap
- 关于HashTable,HashMap和TreeMap的几点心得
- BigInteger转INT
- HDOJ1963完全背包
- C# 结构体 struct
- iOS 开发--github的demo
- iOS中几种数据持久化方案总结
- DBus-glib环境的搭建
- 前端开发中如何选用图片格式
- 如何在linux下共享文件夹
- 移动Web开发技巧汇总
- jquery-validate动态添加表单元素动态验证