题目


Determine whether an integer is a palindrome. An integer is a palindrome when it reads the same backward as forward.

Example1:

  Input:121

  Output:true

Example2:

  Input:-121

  Output:flase

  Explanation:From left to right, it reads -121. From right to left, it becomes 121-. Therefore it is not a palindrome.

Example3:

  Input:10

  Output:flase

  Explanation:Reads 01 from right to left. Therefore it is not a palindrome.

C++思路


思路1:全部逆序

将数字全部逆转,与原数字比较。

思路2:逆转一半的数字

如何判断已经逆转到数字的一半呢?由于对于原数字是除以10,对于逆转的数字是乘以10,如果原来的数字已经小于新生成的数字,那么就表明已经到达数字的一半。此外还要分奇偶讨论,假设a是新生成的数字,b是原数字,则

  • 如果是奇数,则判断a/10 == b
  • 如果是偶数,直接判断 a == b

思路3:字符串分片处理(python)

Tips


回文判断方法

  1. 将数字逆序,并与原数字比较,查看是否一致
  2. 将数字后半部分逆转,将其与前半部分数字比较,查看是否一致。

c++


  • 思路1
class Solution {
public:
bool isPalindrome(int x) {
if(x<0) //所有的负数都不是回文数
return false;
int a = x;
long b = 0;
while(a!=0){
b=b*10 + a%10;
a /= 10;
} if(b == x)
return true;
else
return false;
}
};
  • 思路2
class Solution {
public:
bool isPalindrome(int x){
//特殊情况
if(x < 0 || (x % 10 ==0 && x!=0)){
return 0
}
long a = 0;
int b = x;
while(a < b){
a = a * 10 + b % 10;
b /= 10;
}
return b = =a || b ==a/10;
}
};

Python


  • 思路3
class Solution(object):
def isPalindrome(self, x):
"""
:type x: int
:rtype: bool
"""
b = str(x)
converNum = b[::-1]
return converNum == b
  • 思路1
class Solution(object):
def isPalindrome(self, x):
"""
:type x: int
:rtype: bool
"""
if x<0:
return False
a = 0
b = x
while b>0:
a =a*10+b%10
b = b/10
return a==x

9. Palindrome Number[E]回文数的更多相关文章

  1. palindrome number(回文数)

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

  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(回文数)

    题目等级:Easy 题目描述: Determine whether an integer is a palindrome. An integer is a palindrome when it rea ...

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

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

  5. LeetCode OJ Palindrome Number(回文数)

    class Solution { public: bool isPalindrome(int x) { ,init=x; ) return true; ) return false; ){ r=r*+ ...

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

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

  7. 9. Palindrome Number(回文数)C++

    将int转换为string,注意for循环判断条件的简化 class Solution { public: bool isPalindrome(int x) { ) return false; str ...

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

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

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

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

随机推荐

  1. Android Google 地图 API for Android

    从健康类 app Runkeeper 到游戏 app 精灵宝可梦,位置服务对现代 app 来说越来越重要. 在本文中,我们将创建一个 app,名字就叫做 City Guide.这个 app 允许用户搜 ...

  2. 当接口上配了 FeignClient 和 RequestMapping 两个注解,结果错误提示 重复mapping处理方法

    再接手老文档的时候,发现有这么一个问题 错误显示为: 原文档写法: 解决方法: 这是一个编译时写法的问题,将上方的RequestMapping去掉,然后把路径放在下面的PostMapping 便可以正 ...

  3. 《SLIC Superpixels》阅读笔记

    原始链接:http://blog.csdn.net/jkhere/article/details/16819285 或许有改动,请参考原文! SLIC 超像素(SLICSuperpixels) Rad ...

  4. UVa340(Master-Mind Hints)未完成

    #include<stdio.h> int main() { int num,a[100],i,j,b[100]; while(scanf("%d",&num) ...

  5. 00--Linux常用命令大全

    系统信息 arch 显示机器的处理器架构(1) uname -m 显示机器的处理器架构(2) uname -r 显示正在使用的内核版本 dmidecode -q 显示硬件系统部件 - (SMBIOS ...

  6. 企业级任务调度框架Quartz(1) --企业应用中的任务调度介绍

    由于目前的工作内容为建行CLPM批处理业务的设计工作,所以很好的理解批处理所用的任务调度框架Quartz势在必行:为了能够更好的去服务于工作,也 为了提升自己,所以我学习了Quartz Job Sch ...

  7. 【转】Oracle基础结构认知—oracle物理结构 礼记八目 2017-12-13 20:31:06

    原文地址:https://www.toutiao.com/i6499008214980362765/ oracle数据库启动:oracle服务启动,通过参数文件查找控制文件,启动控制文件,则控制文件调 ...

  8. ack 工具

    ack-tools ack其实就是快速查找工具,但centos在没有这个安装包. 下载安装 cd /tmp git clone https://github.com/dongci/ack.git cd ...

  9. iOS runLoop 原理多线程 总结 NSTimer优化

    可以理解为字面意思:Run 表示运行,Loop 表示循环.结合在一起就是运行的循环的意思.哈哈,我更愿意翻译为『跑圈』.直观理解就像是不停的跑圈. RunLoop 实际上是一个对象,这个对象在循环中用 ...

  10. MySQL 数据库类型