Determine whether an integer is a palindrome. An integer is a palindrome when it reads the same backward as forward.

Example 1:

Input: 121
Output: true
Example 2:

Input: -121
Output: false
Explanation: From left to right, it reads -121. From right to left, it becomes 121-. Therefore it is not a palindrome.
Example 3:

Input: 10
Output: false
Explanation: Reads 01 from right to left. Therefore it is not a palindrome.
Follow up:

Coud you solve it without converting the integer to a string?

法I:逐一比较最左端与最右端的数字

class Solution {
public boolean isPalindrome(int x) {
if(x<0) return false; int left; //the first bit of the inteter
int right; //the last bit of the integer
int divisor = 1; //divisor of each iteration
int tmp = x; //determine the initial value of divisor
while(x/divisor >= 10){
divisor *= 10;
} //check palindrome
while(x>0){
left = x/divisor; //divided by divisor to get the first bit
right = x%10; //mod 10 to get the last bit
if(left != right) return false; x = (x%divisor)/10; //mode divisor to remove the first bit, divided by 10 to remove the last bit
divisor /= 100;
}
return true;
}
}

数字问题注意:

1. 负数

2. 变换后 以0开始 (本题中,如10101)这种情况,除以divisor = 0,mod 10 = 最后那位。

有多少个0,就得经过多少次循环,才能使得divisor的长度和被除数相等。在长度不等的时候,没次都循环末尾需为0,才能符合Palindrome的判断。所以该算法仍然可以验证有0开头的情况。

法II:逆向思维,如果是parlindrome,那么求出的反转数应等于x

这个方法的优点:只遍历了整数长度的一半。

class Solution {
public boolean isPalindrome(int x) {
if(x<0 //negative number
|| (x%10 == 0 && x != 0)) //the check"x == reverseNum/10" has one exception: reverseNum is one-bit number but x isn't, this case only exist in x%10 == 0 && x != 0
return false; int reverseNum = 0;
while(x > reverseNum){
reverseNum = reverseNum * 10 + x%10;
x /= 10;
}
if(x == reverseNum || x == reverseNum/10) return true; //check parlindrome of number with odd or even length
else return false;
}
}

parlindrome问题注意:

1. 数字长度单、双分开讨论。

9. Palindrome Number (JAVA)的更多相关文章

  1. leetcode 第九题 Palindrome Number(java)

    Palindrome Number time=434ms 负数不是回文数 public class Solution { public boolean isPalindrome(int x) { in ...

  2. 65. Reverse Integer && Palindrome Number

    Reverse Integer Reverse digits of an integer. Example1: x =  123, return  321 Example2: x = -123, re ...

  3. 有趣的数-回文数(Palindrome number)

    文章转自http://blog.163.com/hljmdjlln@126/blog/static/5473620620120412525181/ 做LC上的题"Palindrome num ...

  4. 9. Palindrome Number

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

  5. No.009 Palindrome Number

    9. Palindrome Number Total Accepted: 136330 Total Submissions: 418995 Difficulty: Easy Determine whe ...

  6. 【LeetCode】9 & 234 & 206 - Palindrome Number & Palindrome Linked List & Reverse Linked List

    9 - Palindrome Number Determine whether an integer is a palindrome. Do this without extra space. Som ...

  7. HDU 5062 Beautiful Palindrome Number(数学)

    主题链接:http://acm.hdu.edu.cn/showproblem.php? pid=5062 Problem Description A positive integer x can re ...

  8. Reverse Integer - Palindrome Number - 简单模拟

    第一个题目是将整数进行反转,这个题实现反转并不难,主要关键点在于如何进行溢出判断.溢出判断再上一篇字符串转整数中已有介绍,本题采用其中的第三种方法,将数字转为字符串,使用字符串比较大小的方法进行比较. ...

  9. leetcode题解 9. Palindrome Number

    9. Palindrome Number 题目: Determine whether an integer is a palindrome. Do this without extra space. ...

随机推荐

  1. GAN 教程记录

    目标:使G产生的分布sample出来接近D的分布 1.G产生的data是否是database中的图片 a.计算L1 L2相似度 2.GAN与其他生成器相比较,能够生成较为清晰的图片 3.一次itera ...

  2. NOIP2012 Day1 T2国王游戏 洛谷P1080

    第一篇博客啊…… 由于我太弱了,还要去补不全的知识点准备参加人生第一次NOIp,所以第一篇博客就简短一点好了(偷懒就直说吧……) 洛谷P1080传送门 题意概括: 有N对数ai和bi,以及两个数a0和 ...

  3. Linux第一节课学习笔记

    我的目标是考过RHCE 开源软件有使用.复制.传播.收费.修改及创建衍生品自由,其中后二者只有开源软件才有,前四者开闭源共有.

  4. 如何在Python中使用ZeroMQ和Docker构建微服务架构

    @Container容器技术大会将于6月4日在上海光大会展中心国际大酒店举办,来自携程.PPTV.蚂蚁金服.京东.浙江移动.海尔电器.唯品会.eBay.道富银行.麻袋理财等公司的技术负责人将带来实践经 ...

  5. 同一个windows server 部署多个tomcat

    只需要修改tomcat目录下conf下的server.xml文件即可,修改地方有三个,把下面这几个端口修改了为不同的端口即可,例如我把这几个端口统一减1了 <Server port=" ...

  6. Maven项目中在properties 中使用 ${} 来引用pom文件中的属性

    比如在pom文件中定义了属性如下: <jdbc.host.global>127.0.0.1</jdbc.host.global> <jdbc.databasename.g ...

  7. [转]Java泛型

    一. 泛型概念的提出(为什么需要泛型)? 首先,我们看下下面这段简短的代码: 1 public class GenericTest { 2 3 public static void main(Stri ...

  8. python print format

    python print format %o —— oct 八进制 %d —— dec 十进制 %x —— hex 十六进制 1 >>> print('%o' % 20) 2 24 ...

  9. DateUtils时间单元说明

    CompareDate 函数 比较两个日期时间值日期部分的大小 CompareDateTime 函数 比较两个日期时间值的大小 CompareTime 函数 比较两个日期时间值时间部分的大小 Date ...

  10. Batch Close process

    @echo offecho.rem Kill all chrome drivertaskkill /im chromedriver.exe /f pause