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. Android开发学习之路--Service之初体验

    android最后一个组件便是service了,终于学习到最后一个组件了,从年前的开发环境的搭建,到现在学到最后一个组件花了三周的时间,期间记录的点点滴滴,照着书本学习编写的代码都受益匪浅,这里要感谢 ...

  2. Chapter 3 Protecting the Data(4):创建和使用应用程序角色

    原文出处:http://blog.csdn.net/dba_huangzj/article/details/39927713,专题目录:http://blog.csdn.net/dba_huangzj ...

  3. Linux Debugging(六): 动态库注入、ltrace、strace、Valgrind

    实际上,Linux的调试方法非常多,针对不同的问题,不同的场景,不同的应用,都有不同的方法.很难去概括.本篇文章主要涉及本专栏还没有涵盖,但是的确有很重要的方法.本文主要包括动态库注入调试:使用ltr ...

  4. Java Web 高性能开发,第 2 部分: 前端的高性能

    Web 发展的速度让许多人叹为观止,层出不穷的组件.技术,只需要合理的组合.恰当的设置,就可以让 Web 程序性能不断飞跃.Web 的思想是通用的,它们也可以运用到 Java Web.这一系列的文章, ...

  5. Chapter 2 User Authentication, Authorization, and Security(3):保护服务器避免暴力攻击

    原文出处:http://blog.csdn.net/dba_huangzj/article/details/38756693,专题目录:http://blog.csdn.net/dba_huangzj ...

  6. C3P0和DBCP的区别

    C3P0和DBCP的区别 C3P0是一个开源的JDBC连接池,它实现了数据源和JNDI绑定,支持JDBC3规范和JDBC2的标准扩展.目前使用它的开源项目有Hibernate,Spring等.     ...

  7. Linux IPC实践(10) --Posix共享内存

    1. 创建/获取一个共享内存 #include <sys/mman.h> #include <sys/stat.h> /* For mode constants */ #inc ...

  8. 【一天一道LeetCode】#78. Subsets

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...

  9. listview优化(中)

    1,对Imageview使用setTag()方法来解决图片错位问题,这个Tag中设置的是图片的url,然后在加载的时候取得这个url和要加载那position中的url对比,如果不相同就加载,相同就是 ...

  10. 跨平台移动APP开发进阶(四)AngularJS简介

    AngularJS 是一个为动态WEB应用设计的结构框架.它能让你使用HTML作为模板语言,通过扩展HTML的语法,让你能更清楚.简洁地构建你的应用组件. 它的创新点在于,利用 数据绑定 和 依赖注入 ...