题目


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. WPF自定义动画控件 风机

      一:创建WPF项目 二:在项目下添加文件Themes,在此文件下添加新项 ”资源词典“取名为 Generic.xaml  注意大小写,之前遇到因为大小写不对应,导致出错的情况Generic.xam ...

  2. Nagios Windows客户端NSClient++ 0.4.x安装配置

    NSClient++ 0.3.x和NSClient++ 0.4.x的配置完全不一样,官方的文档也没有全部更新.我记录下自己的一些操作.   一.下载安装NSClient++ 1.到http://nsc ...

  3. 异常及String

    异常时描述错误信息的对象,在编码过程中我们会遇到很多异常 例如: 1.java.lang.ArithmeticException 算数异常.算数运算出现错误时抛出 比如用0做除数 2.java.lan ...

  4. RabbitMQ学习之spring配置文件rabbit标签的使用

    下面我们通过一个实例看一下rabbit的使用. 1.实现一个消息监听器ReceiveMessageListener.Java package org.springframework.amqp.core ...

  5. 再谈json

    接上一篇,省市三级联动的例子中,引入了1个QQ网站上的js文件.这个js中构造了一个地址对象,页面上我们所有的操作都跟这个对象关联.今天讨论这种对象怎么构造的问题. 前面写过一篇:浅谈Json数据格式 ...

  6. Git 常用命令速查(转载)

    git branch 查看本地所有分支git status 查看当前状态 git commit 提交 git branch -a 查看所有的分支git branch -r 查看远程所有分支git co ...

  7. 洛谷 P1540 乌龟棋

    第一感觉是定义状态f[n][i][j][k][kk],但这样空间和时间都承受不下.我们可以设状态为f[i][j][k][kk],这样可以省掉一个n,因为我们依据行走步数可以直接算出行走距离. Code ...

  8. 搭建`wenblogic`执行`install`脚本失败

    搭建weblogic服务,前期准备都已经完成,安装包都是已上传,执行install_wls1213.sh脚本,出现以下报错: install_wls1213.sh: line 1: rectory: ...

  9. Project Euler 47 Distinct primes factors( 筛法记录不同素因子个数 )

    题意: 首次出现连续两个数均有两个不同的质因数是在: 14 = 2 × 715 = 3 × 5 首次出现连续三个数均有三个不同的质因数是在: 644 = 22 × 7 × 23645 = 3 × 5 ...

  10. JAVA基础知识复习小结

    集合 Set集合 Set集合的基本特征是元素不允许重复.HashSet不保存元素顺序,LinkedHashSet用链表保持元素的插入顺序,TreeSet可定制排序规则. HashSet的底层是用Has ...