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. IDEA导入springboot项目不能启动

    由于工具没有识别到项目的pom.xml文件,所以需要在右侧的Maven栏目中点击 + 号,选中项目的pom.xml文件,则导入成功.

  2. meat http-equiv 属性详解

    转自 http://kinglyhum.iteye.com/blog/827807 http-equiv 属性提供了 content 属性的信息/值的 HTTP 头. http-equiv 属性可用于 ...

  3. ERP项目实施记录04

    周二做了计划部门的需求调查,提到现有计划(一天计划)的准确率仅有60~70%,每天下来都有30%~40%不能达成. 计划部门提出的需求更多是基于Excel操作思路,要求未来的系统要有更多的" ...

  4. 打造研发效率核心竞争力!第40届MPD软件工作坊北京站议题公开

    同样是做研发,为什么你的效率总是提不上来?都在寻找创新的技术领域,为何别人总能抢占先机?提升自己的研发竞争力,你都有什么方法? 研发效能已经成为软件企业发展非常核心的竞争力.身处在高速发展的软件研发行 ...

  5. windows10安装

    https://www.microsoft.com/zh-cn/software-download/windows10

  6. 配置llama实现impala on yarn-验证未通过,仅以此文作为参考

    以下内容采自网络,目前验证未通过,仅以此作为参考: 简介:早期的Impala版本中,为了使用Impala,我们通常会在以Client/Server的结构在各个集群节点启动impala-server.i ...

  7. Mac开发博客摘录

    https://blog.csdn.net/wangyouxiang/article/details/17855255 https://www.cocoacontrols.com/controls?p ...

  8. Flash builder 、flash cs6、 as 3.0研究

    1.Flash/Actionscript3 载入资源文件方法考 http://zengrong.net/post/1107.htm 2.使用Flash Professional CS5和Flash B ...

  9. ORACLE INSTANCE与EM系统

    Emctl start dbconsole https://192.168.183.132:1158/em/ 复制到游览器进入到em 更改初始化参数值 静态参数: -只能在参数文件中更改 -必须重新启 ...

  10. wait()函数的详细分析

    之前一直没太深入的去理解wait()函数,今天机缘巧合之前又看了看,发现之前没有真正的理解该函数. 众所周知,wait()函数一般用在父进程中等待回收子进程的资源,而防止僵尸进程的产生. (In UN ...