题目


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. 找不到dll原因

    1.没有生成成功: A.代码有错误 B.引用dll有问题(dll生成失败|dll版本不对) C.源文件损坏 2.dll生成路径不对 3.对dll的引用丢失

  2. [Offer收割]编程练习赛39

    公平分队 #pragma comment(linker, "/STACK:102400000,102400000") #include<stdio.h> #includ ...

  3. VueJS 开发常见问题集锦

    由于公司的前端开始转向 VueJS,最近开始使用这个框架进行开发,遇到一些问题记录下来,以备后用. 主要写一些 官方手册 上没有写,但是实际开发中会遇到的问题,需要一定知识基础. 涉及技术栈 CLI: ...

  4. 基于jQuery封装一个瀑布流插件

    /*封装一个瀑布流插件*/ (function($){ $.fn.WaterFall = function(){ /*这是你初始化 调用这个方法的时候的 那个jquery选着到的dom对象 this* ...

  5. Qt与OpenCV结合:图像显示

    参考链接: http://www.cnblogs.com/emouse/archive/2013/03/29/2988717.html 注意:为了防止min max 问题,要把opencv的包含放在包 ...

  6. Kafka学习笔记(1)----Kafka的简介和Linux下单机安装

    1. Kafka简介 Kafka is a distributed,partitioned,replicated commit logservice.它提供了类似于JMS的特性,但是在设计实现上完全不 ...

  7. 创建dynamics CRM client-side (一) - Client-side Events

    这个系列是帮助大家了解dynamics CRM (customer engagement CE) 的client-side 开发. Client-side Events 1. Form OnLoad ...

  8. VS Code编写html(2)

    <html> <head> <title>welcome to myspace</title> <meta charset="utf-8 ...

  9. Java判断字符串中是否含有英文

    实现代码: /* * 判断字符串中是否含有英文,包含返回true */ public boolean isENChar(String string) { boolean flag = false; P ...

  10. twemproxy 安装与配置

    转自:http://blog.csdn.net/u011183653/article/details/21240749 说到twemproxy就不得不说要一下redis,这是因为twemproxy是为 ...