【LeetCode每天一题】Palindrome Number( 回文数字)
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( 回文数字)的更多相关文章
- 【LeetCode】9. Palindrome Number 回文数
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 公众号:负雪明烛 本文关键词:回文数,回文,题解,Leetcode, 力扣,Python ...
- leetcode 9 Palindrome Number 回文数
Determine whether an integer is a palindrome. Do this without extra space. click to show spoilers. S ...
- 9. Palindrome Number 回文 my second leetcode 20170807
Determine whether an integer is a palindrome. Do this without extra space. click to show spoilers. S ...
- LeetCode Problem 9:Palindrome Number回文数
描述:Determine whether an integer is a palindrome. Do this without extra space. Some hints: Could nega ...
- [LeetCode]9. Palindrome Number回文数
Determine whether an integer is a palindrome. An integer is a palindrome when it reads the same back ...
- 【LeetCode】Palindrome Number(回文数)
这道题是LeetCode里的第9道题. 题目说的: 判断一个整数是否是回文数.回文数是指正序(从左向右)和倒序(从右向左)读都是一样的整数. 示例 1: 输入: 121 输出: true 示例 2: ...
- Leetcode 3——Palindrome Number(回文数)
Problem: Determine whether an integer is a palindrome. Do this without extra space. 简单的回文数,大一肯定有要求写过 ...
- Palindrome Number 回文数
判断一个数字是否是回文数,尝试不用其他额外空间. 注意: 负数也有可能成为回文数吗? 如果你想让int转为string,注意不用其他空间这个约束. 你也可以翻转一个int,但是有可能会溢出. ...
- 【LeetCode】9 Palindrome Number 回文数判定
题目: Determine whether an integer is a palindrome. Do this without extra space. Some hints: Could neg ...
随机推荐
- zope.interface 库学习一
由于python没有接口概念,所以zope 提供了个第三方库开源使用,下面简单介绍zope.interface.implementer的使用 直接看例子,下面例子是在twisted里摘录的 接口IRe ...
- linux环境变量配置,转载地址:http://blog.sina.com.cn/rss/1650981242.xml
学习总结 1.Linux的变量种类按变量的生存周期来划分,Linux变量可分为两类:1. 永久的:需要修改配置文件,变量永久生效.2. 临时的:使用export命令行声明即可,变量在关 ...
- IIS 未能加载文件或程序集“System.Web.Mvc, Version=5.2
MVC配置不正确 1. 应用程序池配置成经典模式, 2. 程序池高级设置32位模式. 3.MVC目录设置不网站根目录 ,不要设置为VIEWS目录下.
- OOM
在mnist上测试卷积网络时,显存不够导致下面的报错: ResourceExhaustedError (see above for traceback): OOM when allocating te ...
- 异常详细信息: System.ArgumentException: 不支持关键字: “metadata”。
ASP.NET MVC中报的一个错误... 异常详细信息: System.ArgumentException: 不支持关键字: “metadata”. 其实这个是我修改web.config文件造成的, ...
- 入手Docker容器注意事项:命令结束容器退出
在没有 docker 容器的时候,在终端(terminal)中运行 shell 命令,我们知道当终端退出时(比如关闭终端窗口或退出 ssh 会话),终端中执行的命令也会结束.所以,当我们在终端中执行持 ...
- ros查看摄像头是否打开正常
使用rqt_image_view命令,查看摄像头是否正常输出图像
- web标准 浏览器介绍 开发工具介绍 HTML介绍 HTML颜色介绍 规范 HTML结构详解 {前端之前端初识}
前端之前端初识 前端初识 本节目录 一 web标准 二 浏览器介绍 三 开发工具介绍 四 HTML介绍 五 HTML颜色介绍 六 规范 七 HTML结构详解 一 web标准 web准备介绍: 1. ...
- SQLServer 索引重建
SQL Server 索引重建脚本 在数据的使用过程中,由于索引page碎片过多,带来一些不利的性能问题,我们有时候需要对数据库中的索引进行重组或者重建工作.通常这个阈值为30%,大于30%我们建议进 ...
- mysql帮助命令
HELP contents 查看MySQL命令的使用. eg: HELP 'Data Type' 查看所有的数据类型的使用方法.