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.

判断一个整数是否是回文数,要求是不使用额外空间。

回文数是指正着过去和反着回来都是一样的,如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(回文数)的更多相关文章

  1. leetcode 9 Palindrome Number 回文数

    Determine whether an integer is a palindrome. Do this without extra space. click to show spoilers. S ...

  2. LeetCode Problem 9:Palindrome Number回文数

    描述:Determine whether an integer is a palindrome. Do this without extra space. Some hints: Could nega ...

  3. Leetcode 3——Palindrome Number(回文数)

    Problem: Determine whether an integer is a palindrome. Do this without extra space. 简单的回文数,大一肯定有要求写过 ...

  4. [LeetCode]9. Palindrome Number回文数

    Determine whether an integer is a palindrome. An integer is a palindrome when it reads the same back ...

  5. 【LeetCode】9. Palindrome Number 回文数

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 公众号:负雪明烛 本文关键词:回文数,回文,题解,Leetcode, 力扣,Python ...

  6. Palindrome Number 回文数

    判断一个数字是否是回文数,尝试不用其他额外空间. 注意: 负数也有可能成为回文数吗? 如果你想让int转为string,注意不用其他空间这个约束. 你也可以翻转一个int,但是有可能会溢出.     ...

  7. 【LeetCode】Palindrome Number(回文数)

    这道题是LeetCode里的第9道题. 题目说的: 判断一个整数是否是回文数.回文数是指正序(从左向右)和倒序(从右向左)读都是一样的整数. 示例 1: 输入: 121 输出: true 示例 2: ...

  8. 【LeetCode】9 Palindrome Number 回文数判定

    题目: Determine whether an integer is a palindrome. Do this without extra space. Some hints: Could neg ...

  9. 9. Palindrome Number 回文数的判断

    [抄题]: Determine whether an integer is a palindrome. An integer is a palindrome when it reads the sam ...

  10. [LeetCode] Prime Palindrome 质数回文数

    Find the smallest prime palindrome greater than or equal to N. Recall that a number is prime if it's ...

随机推荐

  1. Volley,小并发网络请求的好帮手

    不得不说,当不了解一件事情的时候,就会像当然的认为,其很神秘.但是当真正的接触到了这些神秘的item,就不会有这种感觉了.作为一个android开发新手的我,刚接触到了Volley这个开源的网络请求框 ...

  2. 发运模块中如何创建Debug 文件

     版本11.5.9到12.x A. 针对发运事务处理或者快速发运产生Debug文件 注意:如果通过发运事务处理执行发放,请参考B部分,下面这部分销售订单发放是格外的设置和日志文件. 1.   每一 ...

  3. JQuery纵向下拉菜单实现心得

    jquery库给我们带来了许多便利,不愧是轻量级的DOM框架,在前面的博文中小编分别对jquery的基础知识以及jquery的一些小demo有一系列的简单介绍,期待各位小伙伴的指导.使用jquery实 ...

  4. MyEclipse2014搭建JSF项目实例

    开发环境:  Windows2008 R2 64位 + MyEclipse Professional 2014 +JDK1.7+Tomcat7.0 搭建步骤: 1.打开MyEclipse2014后新建 ...

  5. SDL2源代码分析1:初始化(SDL_Init())

    ===================================================== SDL源代码分析系列文章列表: SDL2源代码分析1:初始化(SDL_Init()) SDL ...

  6. 精通CSS+DIV网页样式与布局--页面和浏览器元素

    在页面和浏览器中,除了文字.图片.表格.表单等,还有很多各种各样的元素,在上篇博文中,小编主要简单的介绍了一下在CSS中如何设置表格和表单,今天小编主要简单介绍一下丰富的超链接特效.鼠标特效.页面滚动 ...

  7. UNIX环境高级编程——IPC总结

    IPC主要包括:管道,消息队列,信号量,共享内存, 套接字(SOCKET). 一.IPC对象的持久性 每种IPC机制都会借助一种数据结构,这种数据结构的实例称为该IPC机制的对象(相应的,用于同步互斥 ...

  8. C++ Primer 有感(多重继承与虚继承)

    1.多重继承的构造次序:基类构造函数按照基类构造函数在类派生列表中的出现次序调用,构造函数调用次序既不受构造函数初始化列表中出现的基类的影响,也不受基类在构造函数初始化列表中的出现次序的影响.2.在单 ...

  9. Android移动后端服务(BAAS)快速搭建后台服务器之Bmob-android学习之旅(75)

    个人移动开发者的最头疼的问题,就是App的网络后台,包含数据库等,国外目前有比较成熟的解决方案,但是限制于墙的问题,推荐国内的解决方案,比较出名的是Bmob和AVOS cloud和Atom等,这一次我 ...

  10. 【Linux命令】netcat 网络工具的瑞士军刀

    netcat被成为网络工具中的瑞士军刀,之前也没怎么用过,挺惭愧的,那么现在来看看怎么用吧. udp 和 tcp协议都比较好使,至少在测udp的时候,使用telnet感觉很无力呀.(nc 和 netc ...