题目等级: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. 【bzoj4597】 [Shoi2016]随机序列

    可以发现加减号之间可以互相抵消. 真正加到答案里的只有一些前缀积. 记s[i]为a[1]*a[2]*a[3]...*a[i].那s[i]在答案中出现的次数就是2*3^(n-i-1); 修改一个数只会对 ...

  2. 记录一下收集到的clojure相关的东东

    https://github.com/plexus/chestnut 一个用来调试clojurescript的工具,解决 Clojure, ClojureScript, and CSS的reload问 ...

  3. hdu1814 Peaceful Commission——2-SAT

    题目:http://acm.hdu.edu.cn/showproblem.php?pid=1814 第一次的2-SAT,推荐博客:https://blog.csdn.net/jarjingx/arti ...

  4. 如何在 ubuntu 12.04 上安装 skype(转载)

    转自:http://blog.51osos.com/linux/how-to-install-skype-in-ubuntu-12-04/ 添加Canonical Partner Repository ...

  5. bzoj 1631: [Usaco2007 Feb]Cow Party【spfa】

    正反加边分别跑spfa最短路,把两次最短路的和求个max就是答案 #include<iostream> #include<cstdio> #include<queue&g ...

  6. 牛客网NOIP赛前集训营 提高组(第七场)

    中国式家长 2 链接:https://www.nowcoder.com/acm/contest/179/A来源:牛客网 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 262144K, ...

  7. [App Store Connect帮助]四、添加 App 图标、App 预览和屏幕快照(5)移除 App 预览或屏幕快照

    您可以随时移除 App 预览,但仅可在 App 状态为可编辑时才能移除屏幕快照.要了解可编辑的状态,请前往 App 状态. 必要职能:“帐户持有人”职能.“管理”职能.“App 管理”职能或“营销”职 ...

  8. Head Html Css 第二版笔记

    一. 引用 <blockquote>ago aog aogag </blockquote> 则是引用一大段文字并独立显示 二. <a> 创建目的地 <h2&g ...

  9. base64编码上传图片java后台接收实例

    原文地址:http://blog.csdn.net/tonyfreak/article/details/72522855 思路 前台传以data:image/jpeg;base64,开头的base64 ...

  10. HDU 1879(最小生成树)

    #include "iostream" #include "algorithm" #include "cstdio" using names ...