Palindrome Number
2015-09-10 12:48
253 查看
题目:
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.
一种方法可以避免造成溢出,就是直接安装PalidromeString的方法,就直接判断第一个和最后一个,循环往复。这样就不会对数字进行修改,而只是判断而已。
Reference:
http://www.programcreek.com/2013/02/leetcode-palindrome-number-java/ http://www.cnblogs.com/springfor/p/3889214.html
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.
一种方法可以避免造成溢出,就是直接安装PalidromeString的方法,就直接判断第一个和最后一个,循环往复。这样就不会对数字进行修改,而只是判断而已。
public boolean isPalindrome(int x) { //negative numbers are not palindrome if (x < 0) return false; // initialize how many zeros int div = 1; while (x / div >= 10) { div *= 10; } while (x != 0) { int left = x / div; int right = x % 10; if (left != right) return false; x = (x % div) / 10; div /= 100; } return true; }
Reference:
http://www.programcreek.com/2013/02/leetcode-palindrome-number-java/ http://www.cnblogs.com/springfor/p/3889214.html
相关文章推荐
- android从中国天气网获取天气
- CodeForces 69D Dot (游戏+记忆)
- C#读写txt文件的两种方法介绍
- C++用法的学习心得
- c语言学习一
- 数据结构与算法学习之链表的增加、删除
- C强大而灵活的数据
- LA 3882 And Then There Was One 递推
- PowerDesigner修改设计图中文字的字体大小等样式
- ssm中关于basedao和baseMapper的区别
- Golang资源
- UI一揽子计划 1 (UIView UILabel)
- 什么导致了Context泄露:Handler&内部类
- I2C 不良
- Java HTTPS客户端如何处理证书
- 同余定理 专题
- malloc_stats---检查内存泄露的神器
- malloc_stats---检查内存泄露的神器
- malloc_stats---检查内存泄露的神器
- 如何区分cassandra里primary key,partition key,cluster key,clustering key