题目:

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.

链接: http://leetcode.com/problems/palindrome-number/

题解:

可以用除法和模运算rebuild一个结果,然后比较和输入是否相等。这里其实有可能会overflow,就是在 result * 10的时候,但越界的话也会返回一个32位整数,这个整数是 result * 10结果转换为64位里面的low 32位,一般来说与输入不同。所以结果返回false,也能过AC,但是不严谨。

Time Complexity - O(logx), Space Complexity - O(1)。

public class Solution {public boolean isPalindrome(int x) {if(x == 0)return true;int result = 0, temp = x;    while(temp > 0){result = 10 * result + temp % 10;temp /= 10;}return result == x;}
}

另外一种方法可以避免overflow。先用对数计算出x有几位,然后通过数学运算比较第一位和最后一位是否相等,接下来去掉最大的一位和最小的一位,再将digitNum - 2,继续进行计算。

Time Complexity - O(logx), Space Complexity - O(1)。

public class Solution {public boolean isPalindrome(int x) {if(x < 0)return false;int digitNum= (int)(Math.log(x) / Math.log(10));    int left = 0, right = 0;while(x > 0){int powInTens = (int)Math.pow(10, digitNum);left = x / powInTens;right = x % 10;if(left != right)return false;x -= left * powInTens;x /= 10;digitNum -= 2 ;}return true;}
}

二刷:

Java: - Reverse all digits:

public class Solution {public boolean isPalindrome(int x) {if (x < 0) {return false;}int res = 0, tmp = x;while (tmp > 0) {res = res * 10 + tmp % 10;tmp /= 10;}return res == x;}
}

Reverse half digits:

public class Solution {public boolean isPalindrome(int x) {if (x < 0 || (x != 0 && x % 10 == 0)) {return false;}int res = 0;while (x > res) {res = res * 10 + x % 10;x /= 10;}return (res == x) || (x == res / 10);}
}

Python:

class Solution(object):def isPalindrome(self, x):""":type x: int:rtype: bool"""if x < 0 or (x != 0 and x % 10 == 0):return Falseres = 0while x > res:res = res * 10 + x % 10x /= 10return (x == res) or (x == res / 10)

三刷:

Java:

计算全部digits:  设立一个int n = x, 然后我们计算一下n的按位反转数res,最后比较 n == res

public class Solution {public boolean isPalindrome(int x) {if(x < 0) {return false;}int n = x;int res = 0;while (x > 0) {res = res * 10 + x % 10;x /= 10;}return res == n;}
}

计算一半digits:

这里我们要先剪掉特殊情况 x % 10 == 0,这时候假如x不为0的话,肯定不是palindromic number。

之后while循环作比较的条件是  x  > res。 我们只需要计算一半的digits,然后比较是否 x == res, 或者 x == res / 10。

当x是偶数长度的话,我们比较 x == res; 当x时奇数长度的时候,比如 x = 1,这时我们比较的是 x == res / 10。合起来用一个或运算就可以了。

public class Solution {public boolean isPalindrome(int x) {if(x < 0 || (x != 0 && x % 10 == 0)) {return false;}int res = 0;while (x > res) {res = res * 10 + x % 10;x /= 10;}return (x == res || x == res / 10) ;}
}

Reference:

https://leetcode.com/discuss/33500/an-easy-lines-code-only-reversing-till-half-and-then-compare

https://leetcode.com/discuss/23563/line-accepted-java-code-without-the-need-handling-overflow

https://leetcode.com/discuss/12693/neat-ac-java-code-o-n-time-complexity

https://leetcode.com/discuss/65915/python-solution-with-other-variable-introduced-besides-input

转载于:https://www.cnblogs.com/yrbbest/p/4430410.html

9. Palindrome Number相关推荐

  1. 北林oj-算法设计与分析-Tom palindrome number

    描述 Tom is studing math these days. If there is a number X, whose binary form and decimal form are al ...

  2. Bailian4067 回文数字(Palindrome Number)【数学】

    4067:回文数字(Palindrome Number) 总时间限制: 1000ms 内存限制: 65536kB 描述 给出一系列非负整数,判断是否是一个回文数.回文数指的是正着写和倒着写相等的数. ...

  3. 【整数转字符串】LeetCode 9. Palindrome Number

    LeetCode 9. Palindrome Number Solution1: 不利用字符串 class Solution { public:bool isPalindrome(int x) {if ...

  4. 【回文串2】LeetCode 9. Palindrome Number

    LeetCode 9. Palindrome Number Solution1:我的答案 思路一:转化为字符串 class Solution { public:bool isPalindrome(in ...

  5. hdu 5062 Beautiful Palindrome Number(水题)

    题目链接:hdu 5062 Beautiful Palindrome Number 题目大意:略. 解题思路:暴力或者手算都可以,注意手算的话,分别算出1,2,3...位的情况后,答案是累加上去的. ...

  6. js 数组倒序_我用JS刷LeetCode | Day 6 | Palindrome Number

    来公众号「九零后重庆崽儿」,我们一起学前端 回文数: 说明:现阶段的解题暂未考虑复杂度问题 首发地址: 我用JS刷LeetCode | Day 6 | Palindrome Number​www.br ...

  7. LeetCode小白菜笔记[3]:Palindrome Number

    LeetCode小白菜笔记[3]:Palindrome Number 9. Palindrome Number [Easy] 题目:Determine whether an integer is a ...

  8. 9. Palindrome Number*

    9. Palindrome Number* https://leetcode.com/problems/palindrome-number/description/ 题目描述 Determine wh ...

  9. 9—— Palindrome Number

    9. Palindrome Number 回文数 判断一个正整数是不是回文数. 回文数的定义是,将这个数反转之后,得到的数仍然是同一个数. 注意事项 给的数一定保证是32位正整数,但是反转之后的数就未 ...

最新文章

  1. python发送给邮件 转
  2. python3.7.2安装-ubuntu下编译安装Python3.7.2
  3. java中集合的结构list类型
  4. fit,fit_generator的使用区别
  5. 操作系统——内存管理——分段和分页
  6. Java 8 Friday Goodies:Lambda和XML
  7. jQuery系列:Ajax
  8. android gl11,Android 中OpenGL的使用
  9. 复杂网络研究及其前沿概述
  10. 几款常用的Git图形化工具
  11. 记录点滴,善于分享-Microsoft Visio 2016下载与安装教程(全)
  12. ps2021神经ai滤镜无法使用,ps2021没法用神经元滤镜
  13. 酷睿i7 8750h相当于什么水平 i78750h属于什么级别
  14. Nginx-反向代理
  15. nbiot电信平台android,nbiot之bc26 连接电信网联网平台
  16. 判断全角与半角及两者之间的转换
  17. php录音功能,微信开发之录音功能
  18. 安装mysql的初始密码在哪里
  19. Vera++ 默认Rules文件功能解读
  20. Node之Express服务器启动安装与配置

热门文章

  1. 微软私有云分享(R2)5-域升级造成Hyper-V主机无法实时迁移
  2. HTTP Continuation or non-HTTP traffic
  3. 写给程序员的最好的13条建议
  4. 计算两个日期相差几年几个月
  5. AI 学习之路——轻松初探 Python 篇(三)
  6. 这就是爱?英特尔处理器将整合AMD HBM2 GPU
  7. [禅悟人生]清心寡欲, 才是人的真实写照
  8. oracle tuning 工具
  9. 石川es6课程---13-16、generator-认识生成器函数
  10. 根据map中某一字段排序