题目等级:Easy

题目描述:

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?

  题意:判断一个整数是否是回文数。回文数是指正序(从左向右)和倒序(从右向左)读都是一样的整数。


解题思路:

  本题比较简单,一个很直观的做法是我们很熟悉回文字符串的判断,因此可以将整数转为字符串,从而利用回文字符串的判断来判断回文数。当然,题目也提示我们能不能采用别的办法。

  解法一:整数反转后比较

  整数反转后如果和原来的数相同,那么它就一定是回文数,而如何反转一个整数,在上一题中已经解决,可以参考:【LeetCode】7、Reverse Integer(整数反转)

  解法二:只反转一半

  实际上,从解法一也可以看出,么有必要都进行反转,只需要反转整数的后一半,与前一半进行比较即可。

  这里主要需要注意的问题有三个:一是如何判断已经反转了一半,判断条件是:剩下的数字小于已经反转完成的数字。二是需要判断奇数位和偶数位不同的情况,若一共有奇数位,那么反转之后比前一半多一位,需要%10,若一共是奇数位,那么说明反转的后一半与前一半位数相同。三是注意特殊情况:负数都不可能是回文数,最后一位如果是0的数,除0之外的数也不可能是回文数。

class Solution {
public boolean isPalindrome(int x) {
//将后一半反转,与前一半比较
//重点:如何知道我们已经反转到了一半,剩下的数字小于已经反转完成的数字
if(x<0 || (x%10==0 && x!=0)) //负数都不可能是回文数,最后一位是0的除0之外都不可能是回文数
return false;
int reverse=0;
while(x>reverse){
reverse=reverse*10+x%10;
x=x/10;
}
if(x==reverse||x==reverse/10)
return true;
return false;
}
}

  时间复杂度:有多少位就循环多少次的一半,所以是1/2 * lg(n),即以10为底,时间复杂度O(lgn)

  空间复杂度:O(1)

【LeetCode】9、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 3——Palindrome Number(回文数)

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

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

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

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

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

  5. LeetCode Problem 9:Palindrome Number回文数

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

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

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

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

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

  8. Palindrome Number 回文数

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

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

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

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

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

随机推荐

  1. 2015/12/29 Java语言概述 操作中注意事项

    java语言概述 ①版本分类:JavaSE 标准版 桌面开发                 JavaEE 企业版 网络开发                 JavaME 移动版 嵌入式开发(塞班系统 ...

  2. Android下拉刷新

    以下是我自己花功夫编写了一种非常简单的下拉刷新实现方案,现在拿出来和大家分享一下.相信在阅读完本篇文章之后,大家都可以在自己的项目中一分钟引入下拉刷新功能 最近项目中需要用到ListView下拉刷新的 ...

  3. 【POJ 1330】 Nearest Common Ancestors

    [题目链接] 点击打开链接 [算法] 倍增法求最近公共祖先 [代码] #include <algorithm> #include <bitset> #include <c ...

  4. Python基础第二天

    一.内容 二.练习 练习1 题目:已知msg='hello knight 666'编写for循环,利用索引遍历出每一个字符 图示: 代码: msg = 'hello knight 666' msg_l ...

  5. 【转】整套完整安全的API接口解决方案

    原文地址:http://www.cnblogs.com/hubro/p/6248353.html 在各种手机APP泛滥的现在,背后都有同样泛滥的API接口在支撑,其中鱼龙混杂,直接裸奔的WEB API ...

  6. 使用Google Closure Compiler全力压缩代码(转)

    JavaScript压缩代码的重要性不言而喻,如今的压缩工具也有不少,例如YUI Compressor,Google Closure Compiler,以及现在比较红火的UglifyJS.Uglify ...

  7. Python 常用算法记录

    一.递归 汉诺塔算法:把A柱的盘子,移动到C柱上,最少需要移动几次,大盘子只能在小盘子下面 1.当盘子的个数为n时,移动的次数应等于2^n – 1 2.描述盘子从A到C: (1)如果A只有一个圆盘,可 ...

  8. Objective-C 继承与类

    创建: 2018/01/20 完成: 2018/01/21 更新: 2018/01/22 标题前增加 [Objective-C]  继承的概念  父类与子类 ●继承: 继承其他类 ●父类: 被继承的类 ...

  9. 洛谷 P1865 A % B Problem(求区间质数个数)

    题目背景 题目名称是吸引你点进来的 实际上该题还是很水的 题目描述 区间质数个数 输入输出格式 输入格式: 一行两个整数 询问次数n,范围m 接下来n行,每行两个整数 l,r 表示区间 输出格式: 对 ...

  10. [C++ STL] 常用算法总结

    1 概述 STL算法部分主要由头文件<algorithm>,<numeric>,<functional>组成.要使用 STL中的算法函数必须包含头文件<alg ...