前言

 

【LeetCode 题解】系列传送门:  http://www.cnblogs.com/double-win/category/573499.html

 

1.题目描述

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

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.

2. 题意

判断一个整数是否是回文数。要求使用常量空间。

提示:

(1)负数是否是回文数?

(2)注意常量空间复杂度。将数字转换成字符串是不可行的。

3. 思路

如果将整个数字转换再判断是否是回文数,那么就可能出现反转之后的数字越界。

延续这个思路,能否通过某些技巧来避免越界呢?先看下面的例子:

(1) 1234321 / 12344321

将数字分成等长的两部分: 1234 和 4321。那么可以看出 4321 反转之后的数字为1234. 两者相等,所以1234321为回文数。

(2) 12345321

将数字分成等长的两部分: 1234 和 5321。那么可以看出 5321 反转之后的数字为1235.

由于1234!=1235,所以12345321不是回文数。

从上面两个例子可以看出。在处理一个数字是否是回文数的过程中,没有必要将整个数字反转。而只需要判断数字的前后等长的两部分时候相等即可。

那么如何在常量空间复杂度内,将数字分成前后两部分呢?

记 需要判断的数字为x,其前一部分为firstpart=x,后一部分为secondpart=0.

采取依次取firstpart的末位,将其添加到secondpart的尾部的方式,直到firstpart<=secondpart.

firstpart secondpart
1234321 0
123432 1
12343 12
1234 123
123 1234

当firstpart<secondpart时,只需反过来将secondpart最后一位转移到firstpart末位即可。

tmp=1234%10=4;

firstpart=firstpart*10+tmp=123*10+4=1234。

此时secondpart也为1234.

因此1234321为回文数。

4: 解法

class Solution {
public:
bool isPalindrome(int x) {
int first=x,second=0,tmp;
if(x==0) return true; //zero
if(x<0|| x%10==0) return false; //negative number or the number is dividable by 10 while(first>second){ // reverse the number
tmp=first%10;
second= second*10+tmp;
first/=10;
}
if(first==second) return true;
else{ // handle the number with odd digits
tmp = second%10;
first=first*10+tmp;
if(first==second) return true;
else return false;
}
return false;
}
};

作者:Double_Win
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文链接,否则作者保留追究法律责任的权利。  若本文对你有所帮助,您的关注推荐是我们分享知识的动力!

[LeetCode 题解]:Palindrome Number的更多相关文章

  1. leetcode题解||Palindrome Number问题

    problem: Determine whether an integer is a palindrome. Do this without extra space. click to show sp ...

  2. LeetCode题解——Palindrome Number

    题目: 判断一个数字是不是回文数字,即最高位与最低位相同,次高位与次低位相同,... 解法: 求出数字的位数,然后依次求商和求余判断是否相等. 代码: class Solution { public: ...

  3. Leetcode 9. Palindrome Number(水)

    9. Palindrome Number Easy Determine whether an integer is a palindrome. An integer is a palindrome w ...

  4. Leetcode练习题 Palindrome Number

    9. Palindrome Number Question: Determine whether an integer is a palindrome. An integer is a palindr ...

  5. leetcode 9 Palindrome Number 回文数

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

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

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

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

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

  8. [LeetCode][Python]Palindrome Number

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com'https://oj.leetcode.com/problems/palindr ...

  9. LeetCode——9. Palindrome Number

    一.题目链接:https://leetcode.com/problems/palindrome-number/ 二.题目大意: 给定一个整数,判断它是否为一个回文数.(例如-12,它就不是一个回文数: ...

  10. 蜗牛慢慢爬 LeetCode 9. Palindrome Number [Difficulty: Easy]

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

随机推荐

  1. Windows XP with SP3大客户免激活日文版

    原贴地址:http://www.humin.com.cn/ja_windows_xp_professional_with_service_pack_3_x86_dvd_vl_x14-74058-iso ...

  2. Ubuntu dns

    在Ubuntu系统网络设备启动的流程中,会依赖/etc/network/interface的配置文件初始化网络接口,所以直接在/etc/network/interface之中配置好对应的dns服务器会 ...

  3. form表单的默认提交行为

    一 如果<form></form>表单中只有一个<input type="text"/>,则使文本框获取焦点,并单击回车,form会自动提交. ...

  4. DOS 命令集锦——最常用命令

    一. 常用命令: cd 改变当前目录   sys 制作DOS系统盘 (电脑入门到精通网 www.58116.cn) copy 拷贝文件  del 删除文件 deltree 删除目录树    dir 列 ...

  5. iOS 基于MVC设计模式的基类设计

    iOS 基于MVC设计模式的基类设计 https://www.jianshu.com/p/3b580ffdae00

  6. mysql只保留一条有效数据,删除其他重复的数据

    delete from TableName where id in( SELECT ID FROM(SELECT * FROM TableName t0WHERE(t0.Field1,t0.Field ...

  7. Alluxio/Tachyon如何发挥lineage的作用?

    在Spark的RDD中引入过lineage这一概念.指的是RDD之间的依赖.而Alluxio则使用lineage来表示文件之间的依赖.在代码层面,指的是fileID之间的依赖. 代码中的注释指出: * ...

  8. laravel表单提交

    1.控制器->路由->视图 2.视图 3.控制器

  9. Byte字节与位

    位(bit)字节(byte)一字节是8位所以2Byte是16位二进制

  10. Mockplus微信小程序上线!扫一扫轻松查看原型!

    Mockplus团队发布了Mockplus微信小程序. 从现在起,你无需下载Mockplus移动端,用微信扫一扫二维码,即可在微信中打开并查看原型.Mockplus微信小程序,无需安装.卸载,不占用手 ...