palindrome number(回文数)
Determine whether an integer is a palindrome. Do this without extra space.
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.
判断一个整数是否是回文数,要求是不使用额外空间。
回文数是指正着过去和反着回来都是一样的,如1221,12121.
本来可以转成字符串,然后从前后开始比较即可,但是题目说不能使用额外空间,所以这一条不成立。
可以旋转这个整数,旋转回来的数和这个数相等就是回文数。考虑到旋转会导致溢出,这时返回false即可,因为x是int型,如果是回文数,它也是x,不可能溢出。
还可以简洁一点,因为是前半部分和后半部分的旋转是一样的,所以可以旋转一半,这时比较。当然,这时要注意x由奇数个数字组成时和偶数个数字组成的情况,
class Solution {
public boolean isPalindrome(int x) {
if(x<0||(x!=0&&x%10==0))
return false;
int res=0;
/*
//全部旋转
int a=x;
while(x>0){
int tail=x%10;
int newRes=res*10+tail;
if((newRes-tail)/10!=res)
return false;
res=newRes;
x=x/10;
}
return a==res;
*/
//旋转整数,只要旋转一半就行了,旋转一半就不会出现溢出问题
while(x>res){
res=res*10+x%10;
x=x/10;
}
return x==res||res/10==x; //分别考虑x是偶数个数字组成,和x是奇数个数字组成
}
}
palindrome number(回文数)的更多相关文章
- leetcode 9 Palindrome Number 回文数
Determine whether an integer is a palindrome. Do this without extra space. click to show spoilers. S ...
- LeetCode Problem 9:Palindrome Number回文数
描述:Determine whether an integer is a palindrome. Do this without extra space. Some hints: Could nega ...
- Leetcode 3——Palindrome Number(回文数)
Problem: Determine whether an integer is a palindrome. Do this without extra space. 简单的回文数,大一肯定有要求写过 ...
- [LeetCode]9. Palindrome Number回文数
Determine whether an integer is a palindrome. An integer is a palindrome when it reads the same back ...
- 【LeetCode】9. Palindrome Number 回文数
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 公众号:负雪明烛 本文关键词:回文数,回文,题解,Leetcode, 力扣,Python ...
- Palindrome Number 回文数
判断一个数字是否是回文数,尝试不用其他额外空间. 注意: 负数也有可能成为回文数吗? 如果你想让int转为string,注意不用其他空间这个约束. 你也可以翻转一个int,但是有可能会溢出. ...
- 【LeetCode】Palindrome Number(回文数)
这道题是LeetCode里的第9道题. 题目说的: 判断一个整数是否是回文数.回文数是指正序(从左向右)和倒序(从右向左)读都是一样的整数. 示例 1: 输入: 121 输出: true 示例 2: ...
- 【LeetCode】9 Palindrome Number 回文数判定
题目: Determine whether an integer is a palindrome. Do this without extra space. Some hints: Could neg ...
- 9. Palindrome Number 回文数的判断
[抄题]: Determine whether an integer is a palindrome. An integer is a palindrome when it reads the sam ...
- [LeetCode] Prime Palindrome 质数回文数
Find the smallest prime palindrome greater than or equal to N. Recall that a number is prime if it's ...
随机推荐
- Hibernate配置文件current_session_context_class的意思
转自:http://shuaigg-babysky.iteye.com/blog/563423 此设置的作用如下: What does sessionFactory.getCurrentSession ...
- shell入门之函数应用
最近在学习shell编程,文中若有错误的地方还望各位批评指正. 先来看一个简单的求和函数 #!/bin/bash #a test about function f_sum 7 8 function f ...
- UNIX网络编程——UDP 的connect函数(改进版)
上一篇我们提到,除非套接字已连接,否则异步错误是不会返回到UDP套接字的.我们确实可以给UDP套接字调用connect,然而这样做的结果却与TCP连接大相径庭:没有三次握手.内核只是检查是否存在立即可 ...
- Cocos2D iOS之旅:如何写一个敲地鼠游戏(四):创建TexturePacker自动脚本
大熊猫猪·侯佩原创或翻译作品.欢迎转载,转载请注明出处. 如果觉得写的不好请告诉我,如果觉得不错请多多支持点赞.谢谢! hopy ;) 免责申明:本博客提供的所有翻译文章原稿均来自互联网,仅供学习交流 ...
- java linux ImageIO 验证码在一段时间以后出不来 问题总结
最近在测试上布署的项目经常性的出现验证码过了一段时间以后出不来的情况,耐心找了一下,最后在上级的指导下发现了报错,其实说真的,我自己也找到了这个报错,只是没有当一回事.因为这个验证码的东西不是我写的, ...
- python 内存数据库与远程服务
python 内存数据库与远程服务 需要import的python 内存数据库代码参考下面的链接: http://blog.csdn.net/ubuntu64fan/article/details/5 ...
- UNIX环境高级编程——可靠信号与不可靠信号
在早期的UNIX中信号是不可靠的,不可靠在这里指的是:信号可能丢失,一个信号发生了,但进程却可能一直不知道这一点. 现在Linux 在SIGRTMIN实时信号之前的都叫不可靠信号,这里的不可靠主要是不 ...
- 设计模式之——工厂模式(C)
本文是在学习中的总结,欢迎转载但请注明出处:http://blog.csdn.net/pistolove/article/details/41219129 主要介绍抽象工厂模式,以及工厂方法和抽象工厂 ...
- Oracle 11g Articles
发现一个比较有意思的网站,http://www.oracle-base.com/articles/11g/articles-11g.php Oracle 11g Articles Oracle Dat ...
- C语言之统计输入字符数量
这个程序市委了统计所输入的数字或者英文字母的数字的数量,当然稍加改动便可以统计特殊字符的个数,在此不再冗叙. 代码如下: #include <iostream> using namespa ...