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

Example 1:          Input: 121            Output: true

Example 2:          Input: -121          Output: false                Explanation: From left to right, it reads -121. From right to left, it becomes 121-. Therefore it is not a palindrome.

Example 3:         Input: 10              Output: false                Explanation: Reads 01 from right to left. Therefore it is not a palindrome.

Follow up: Coud you solve it without converting the integer to a string?

思路


这道题最简单的解决办法是将数字转换成字符串,从头和尾向中间进行遍历。可以得出结果。但是在题中给出了可不可以不适用转化字符的办法来解决。这里我想到了使用辅助空间来解决。 申请一个堆和队列。然后依此将数字加入其中。然后再逐个对比数字是否相等。相等则时回文。时间复杂度为O(log 10 n), 空间复杂度为O(n).

图示步骤


实现代码


  

 class Solution(object):
def isPalindrome(self, x):
"""
:type x: int
:rtype: bool
"""
if x < : # 如果数字小于0,直接返回
return False
tem1, tem2 = [], [] # 设置两个辅助列表来模拟栈和队列。
while x: # 堆数字进行遍历
tem = x %
tem1.append(tem)
tem2.append(tem)
x //= 10
i, end = , len(tem1)-1
while i < len(tem1) and end >= : # 队列和栈来依此访问。
if tem1[i] != tem2[end]:
return False # 有一个不相等直接返回。
i +=
end -=
return True

【LeetCode每天一题】Palindrome Number( 回文数字)的更多相关文章

  1. 【LeetCode】9. Palindrome Number 回文数

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 公众号:负雪明烛 本文关键词:回文数,回文,题解,Leetcode, 力扣,Python ...

  2. leetcode 9 Palindrome Number 回文数

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

  3. 9. Palindrome Number 回文 my second leetcode 20170807

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

  4. LeetCode Problem 9:Palindrome Number回文数

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

  5. [LeetCode]9. Palindrome Number回文数

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

  6. 【LeetCode】Palindrome Number(回文数)

    这道题是LeetCode里的第9道题. 题目说的: 判断一个整数是否是回文数.回文数是指正序(从左向右)和倒序(从右向左)读都是一样的整数. 示例 1: 输入: 121 输出: true 示例 2: ...

  7. Leetcode 3——Palindrome Number(回文数)

    Problem: Determine whether an integer is a palindrome. Do this without extra space. 简单的回文数,大一肯定有要求写过 ...

  8. Palindrome Number 回文数

    判断一个数字是否是回文数,尝试不用其他额外空间. 注意: 负数也有可能成为回文数吗? 如果你想让int转为string,注意不用其他空间这个约束. 你也可以翻转一个int,但是有可能会溢出.     ...

  9. 【LeetCode】9 Palindrome Number 回文数判定

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

随机推荐

  1. nginx 根据端口不同实现负载均衡

    upstream www.abc.com { server www.mynginx.com:91; server www.mynginx.com:92; }server { listen 80; se ...

  2. Robot Framework使用For循环

    1.普通的For循环 在一个普通的For循环中,循环开始的关键字是 :FOR ,其中的:用于与一般关键字做区分,对于循环结构体内的每一行,使用 \ 作为改行的行首关键字.对于循环中的变量,可以在 IN ...

  3. ubuntu经常断网、掉线、上不去网的原因

    方案一: ubuntu经常断网.掉线.上不去网的原因,很可能是因为IPv6的关系,Ubuntu默认开启IPv6,在“设置--wifi--齿轮图标”中关掉就可以了. 经我环境测试,此方法无效 方案二: ...

  4. Linux下的几种IPC方式及其C语言实现

    写在前面:本博客为本人原创,严禁任何形式的转载!本博客只允许放在博客园(.cnblogs.com),如果您在其他网站看到这篇博文,请通过下面这个唯一的合法链接转到原文! 本博客全网唯一合法URL:ht ...

  5. Chap5:操作文件和目录[The Linux Command Line]

    Wildcards Wildcard Meaning * Matches any characters ? Matches any single character [characters] Matc ...

  6. [dpdk] SDK编译-简单扼要版

    0. 前提: 环境是CentOS7,archlinux编译有问题,不知道却什么. 1. 解压: [root@dpdk dpdk]# tar Jxf dpdk-2.2.0.tar.xz 2. 设置环境变 ...

  7. LeetCode 867 Transpose Matrix 解题报告

    题目要求 Given a matrix A, return the transpose of A. The transpose of a matrix is the matrix flipped ov ...

  8. java jdk安装配置

    1. 配置java_home 2. path添加: %JAVA_HOME%\bin;%JAVA_HOME%\jre\bin; 3. 添加CLASSPATH路径: .;%Java_Home%\bin;% ...

  9. navicat新建用户,并赋予权限

    一.新建用户 二.设置主机和密码 主机%的意思是允许用户本地登入和远程登入. 三.选择表或者视图 右键,选择设置权限 四. 选择添加权限. 五. 选择用户以及Select  和  Show View ...

  10. Maven项目常见的小问题

    pom.xml文件头报错 场景 例Failure to transfer org.apache.maven.plugins:maven-surefire-plugin:pom:2.12.4 from ...