我现在在做一个叫《leetbook》的开源书项目,把解题思路都同步更新到github上了,需要的同学可以去看看
地址:https://github.com/hk029/leetcode
这个是书的地址:https://hk029.gitbooks.io/leetbook/

009. Palindrome Number[E]

问题:

Determine whether an integer is a palindrome. Do this without extra space.

思路

这里说不用额外的空间意思是不用O(n)的空间,O(1)的还是可以用的,不然循环都不好写。。

思路1

简单的思路 就是把数字逆转,然后判断逆转后的数字跟原来数字是不是一样的。

class Solution {
public:
bool isPalindrome(int x) {
if(x < 0) return false;
int r=0,t;
t = x;
while(t != 0)
{
r =r*10 + t%10;
t /=10;
}
return r == x; }
};

思路2

但是其实,不用把数字逆转完再判断,因为如果是回文数字,那么只要逆转一半看是否满足回文条件就行了。

设新数为r,原来数为x,每次:

x = x/10,r = r*10 + x%10;
  • 如何到一半停止?

    • 如果x <= r时可以停止了(至少到了一半)
    • 如果是偶数长度,并且是回文,那么刚好可以到x == r
    • 如果为奇数长度,并且是回文,那么x < r,这时候r刚好比x多一位数
  • 停止后判断是否是回文

    • 如果是偶数长度,很简单判断 r == x
    • 如果是奇数长度,要判断 r/10 == x

注意:这里有一些小问题。

  • 当尾数为0的情况。尾数为0会导致r的增长少1位数(因为0*10 = 0)。

    比如:110不是回文,最后停止r = 1 x =1 但是 r == x

  • 当数字小于0的时候,也是不满足回文的条件的。

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

《LeetBook》leetcode题解(9):Palindrome Number[E]——回文数字的更多相关文章

  1. LeetCode 9. Palindrome Number (回文数字)

    Determine whether an integer is a palindrome. Do this without extra space. 题目标签:Math 题目给了我们一个int x, ...

  2. [LeetCode] Palindrome Number 验证回文数字

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

  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 9 Palindrome Number(回文数字判断)

    Long Time No See !   题目链接https://leetcode.com/problems/palindrome-number/?tab=Description   首先确定该数字的 ...

  5. Leetcode 9. Palindrome Number(判断回文数字)

    Determine whether an integer is a palindrome. Do this without extra space.(不要使用额外的空间) Some hints: Co ...

  6. 从0开始的LeetCode生活—9. Palindrome Number(回文数)

    题目大意: 判断输入的数字是不是回文数.所谓回文数就是正反读都一样的数字,比如说11,121,1221这样子的数字.负数不会是回文数. 解题思路: 思路一:如果这个数是负数,则返回false,否则用一 ...

  7. [LeetCode]9. Palindrome Number判断回文数字

    /* 查看网上的思路有两种: 1.每次取两边的数,然后进行比较 2.取数的倒置数,进行比较 */ public boolean isPalindrome1(int x) { if (x<0) r ...

  8. [LeetCode] 409. Longest Palindrome 最长回文

    Given a string which consists of lowercase or uppercase letters, find the length of the longest pali ...

  9. leetcode题解 9. Palindrome Number

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

随机推荐

  1. (最小生成树)Eddy's picture -- hdu -- 1162

    链接: http://acm.hdu.edu.cn/showproblem.php?pid=1162 Time Limit: 2000/1000 MS (Java/Others)    Memory ...

  2. Opengl的TOOL收集

    1. http://my.oschina.net/sweetdark 不错的opengl学习网站 2, 优秀博客 网址: http://www.zwqxin.com/archives/opengl/s ...

  3. handsontable-developer guide-cell type

    单元格类型:这里有很多没见过的用法,得好好总结一下 //预定义的类型Text Numeric Checkbox Date Select Dropdown Autocomplete Password H ...

  4. Visual Studio 2017(VS2017) 企业版 Enterprise 注册码

    Visual Studio 2017(VS2017) 企业版 Enterprise 注册码:NJVYC-BMHX2-G77MM-4XJMR-6Q8QF 终于等到你,最强 IDE Visual Stud ...

  5. RoadFlow ASP.NET Core工作流配置文件说明

    工作流配置文件及说明如下: { "Logging": { "LogLevel": { "Default": "Warning&qu ...

  6. sharepoint 通过数据库擅长列表项

    select *from [dbo].[AllLists] where tp_Title='Pages' and tp_WebId='68BDFC9A-4E0C-425E-9985-573CD6716 ...

  7. SoundPool跑套图片

  8. java学习笔记—EL表达式(38)

    EL表达式 EL即Expression Language,主要的任务是帮助开发者简化获取域属性. 但是一般情况下需要使用EL和JSTL结合使用. 语法: ${ // 隐含对象|直接写域中的属性 }  ...

  9. Java io流完成复制粘贴功能

    JAVA 中io字节输入输出流 完成复制粘贴功能: public static void main(String[] args) throws Exception{        // 创建输入流要读 ...

  10. html基础整理(01居中 盒子问题)

    01 文字居中 将一段文字置于容器的水平中点,只要设置text-align属性即可: text-align:center; 02 容器水平居中   先为该容器设置一个明确宽度,然后将margin的水平 ...