【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 ...
随机推荐
- Python零基础入门学习 作者:小甲鱼
temp = input('不妨想一想小甲鱼现在心里想的哪一个数字:') guess = int(temp) if guess == 8: print('你是小甲鱼心里的蛔虫吗?') print('哼 ...
- day_6.6 py
Tftp 下载器演示 2018-6-6 14:11:09 #!/usr/bin/env python #!--*--coding:utf-8 --*-- #!@Time :2018/6/6 11:55 ...
- Flask web开发之路二
今天创建第一个flask项目,主app文件代码如下: # 从flask这个框架导入Flask这个类 from flask import Flask #初始化一个Flask对象 # Flasks() # ...
- ABP之事件总线(5)
前面已经对Castle Windsor的基本使用进行了学习,有了这个基础,接下来我们将把我们的事件总线再次向ABP中定义的事件总线靠近.从源码中可以知道在ABP中定义了Dictionary,存放三种类 ...
- Oracle客户端的安装与远程连接配置
在继续Oracle客户端的安装之前,需要分析一下Oracle客户端与数据库服务器之间的连接机制. 一.Oracle客户端与服务器端的通讯机制 1.Oracle Net协议 如下图所示,Oracle通过 ...
- pfSense软件防火墙安装配置
一,说明 1.1 pfSense是什么 pfSense是基于FreeBSD的.开源中最为可靠(World's Most Trusted Open Source Firewall)的.可与商业级防火墙一 ...
- Copycat - StateMachine
看下用户注册StateMachine的过程, CopycatServer.Builder builder = CopycatServer.builder(address); builder.withS ...
- windows SysinternalsSuite
procdump -ma -i c\dumps 捕获系统所有程序的崩溃 SysinternalsSuite autoruns是个什么鬼
- 抽屉之Tornado实战(1)--分析与架构
抽屉之Tornado实战(1)--分析与架构 项目模拟地址:http://dig.chouti.com/ 知识点应用: AJAX 用于偷偷发请求 原生ajax jQuery ajax($.aj ...
- 《HTTP - http首部信息》
推荐一首歌 - 僕が死のうと思ったのは (曾经我也想过一了百了) 也就听了几十遍而已 经历一番波折,终于正式到了北京. 刚开始是很艰难的,多走两步就好了,不是么. 1:首部字段 Cache-Contr ...