作者: 负雪明烛
id: fuxuemingzhu
个人博客: http://fuxuemingzhu.cn/


题目地址:https://leetcode.com/problems/super-palindromes/description/

题目描述

Let’s say a positive integer is a superpalindrome if it is a palindrome, and it is also the square of a palindrome.

Now, given two positive integers L and R (represented as strings), return the number of superpalindromes in the inclusive range [L, R].

Example 1:

Input: L = “4”, R = “1000”
Output: 4
Explanation: 4, 9, 121, and 484 are superpalindromes.
Note that 676 is not a superpalindrome: 26 * 26 = 676, but 26 is not a palindrome.

Note:

  1. 1 <= len(L) <= 18
  2. 1 <= len(R) <= 18
  3. L and R are strings representing integers in the range [1, 10^18).
  4. int(L) <= int(R)

题目大意

找出在[L, R]双闭区间内,所有的超级回文数个数。超级回文数是指本身是回文数,并且它的算数平方根也是回文数。

解题方法

BFS解法

暴力求解的话一定超时,很明显超级回文数是很稀少的,我们还是想想怎么能找规律吧。这是一些超级回文数:

# n, n^2
(1, 1)
(2, 4)
(3, 9)
(11, 121)
(22, 484)
(101, 10201)
(111, 12321)
(121, 14641)
(202, 40804)
(212, 44944)
(1001, 1002001)
(1111, 1234321)
(2002, 4008004)

可以注意到,在除了(1,4,9)之外的其他超级回文数的算数平方根都是有0,1,2组成,而且两头都是由1或者2构成。

所以可以想到BFS的解法,我们使用一个队列保存的是回文的算数平均数n,然后我们拿出队列的每个元素,像中间部分插入0,1,2作为候选的n(保证仍然是回文),然后把候选的n再次放入到队列中去,如果算数平均数n的平方大于R了,就不用拿他计算新的数字了。

最后还需要把1,2,3给放入到候选里面去,也要判断候选的回文算数平均数的平方是不是回文数,如果是的话,结果加一。

时间复杂度不会算,空间复杂度不会算.超过65%.

class Solution:
def superpalindromesInRange(self, L, R):
"""
:type L: str
:type R: str
:rtype: int
"""
que = collections.deque(["11", "22"])
candi = set()
while que:
size = len(que)
for _ in range(size):
p = que.popleft()
candi.add(p)
if int(p) ** 2 > int(R):
continue
for j in ["0", "1", "2"]:
q = (p[:len(p)//2] + j + p[len(p)//2:])
que.append(q)
candi.add("1")
candi.add("2")
candi.add("3")
res = 0
for cand in candi:
if int(L) <= int(cand) ** 2 <= int(R) and self.isPalindromes(int(cand) ** 2):
res += 1
return res def isPalindromes(self, s):
s = str(s)
N = len(s)
for l in range(1, N // 2):
if s[l] != s[N - 1 - l]:
return False
return True

相似题目

参考资料

https://leetcode.com/problems/super-palindromes/discuss/171450/Python-AC-bfs-detail-explanation

日期

2018 年 11 月 3 日 —— 雾霾的周六

【LeetCode】906. Super Palindromes 解题报告(Python)的更多相关文章

  1. [LeetCode] 906. Super Palindromes 超级回文数

    Let's say a positive integer is a superpalindrome if it is a palindrome, and it is also the square o ...

  2. 【LeetCode】120. Triangle 解题报告(Python)

    [LeetCode]120. Triangle 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址htt ...

  3. LeetCode 1 Two Sum 解题报告

    LeetCode 1 Two Sum 解题报告 偶然间听见leetcode这个平台,这里面题量也不是很多200多题,打算平时有空在研究生期间就刷完,跟跟多的练习算法的人进行交流思想,一定的ACM算法积 ...

  4. 【LeetCode】Permutations II 解题报告

    [题目] Given a collection of numbers that might contain duplicates, return all possible unique permuta ...

  5. 【LeetCode】Island Perimeter 解题报告

    [LeetCode]Island Perimeter 解题报告 [LeetCode] https://leetcode.com/problems/island-perimeter/ Total Acc ...

  6. 【LeetCode】01 Matrix 解题报告

    [LeetCode]01 Matrix 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/01-matrix/#/descripti ...

  7. 【LeetCode】Largest Number 解题报告

    [LeetCode]Largest Number 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/largest-number/# ...

  8. 【LeetCode】Gas Station 解题报告

    [LeetCode]Gas Station 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/gas-station/#/descr ...

  9. LeetCode: Unique Paths II 解题报告

    Unique Paths II Total Accepted: 31019 Total Submissions: 110866My Submissions Question Solution  Fol ...

随机推荐

  1. 如何利用nrfjprog.exe读写nrf51的flash

    版权声明:本文为博主原创文章,未经博主允许不得转载.   1.目的 为了方便平时在开发中的调试,验证一些想法是否正确. 2.平台: Jlink version:v5.02c nrf51822硬件板等. ...

  2. C/C++ Qt 数据库与ComBox多级联动

    Qt中的SQL数据库组件可以与ComBox组件形成多级联动效果,在日常开发中多级联动效果应用非常广泛,例如当我们选择指定用户时,我们让其在另一个ComBox组件中列举出该用户所维护的主机列表,又或者当 ...

  3. accent, access, accident

    accent A colon (:) is used to represent a long vowel [元音], e.g. sheet /ʃiːt/ and shit /ʃit/. The wor ...

  4. day05 连表查询与子查询

    day05 连表查询与子查询 昨日内容回顾 表关系之一对一 换位思考之后得出两边都是不可以 要么是没有关系,要么是一对一 一对一的表关系外键虽然建在哪个都可以,但是建议建在查询频率多的表上 # 外键其 ...

  5. Flink(八)【Flink的窗口机制】

    目录 Flink的窗口机制 1.窗口概述 2.窗口分类 基于时间的窗口 滚动窗口(Tumbling Windows) 滑动窗口(Sliding Windows) 会话窗口(Session Window ...

  6. 【分布式】Zookeeper客户端基本的使用

    与mysql.redis等软件一样,zookeeper的软件包中也提供了客户端程序用于对服务器上的数据进行操作.本节我们就来学习zookeeper客户端的使用方法.不过在详细讲解zk客户端的使用方法之 ...

  7. 【Linux】【Basis】Kernel

    Linux Kernel:               CentOS启动流程:POST --> Bootloader(BIOS, MBR) --> Kernel(initrd) --> ...

  8. 【Linux】【Basis】Grub

    GRUB(Boot Loader):   1. grub: GRand Unified Bootloader grub 0.x: grub legacy grub 1.x: grub2   2. gr ...

  9. 对于HTML和XML的理解

    1.什么是HTML??? HTML就是 超文本标记语言(超文本含义:超过文本 --图片 .视频.音频. 超链接) 2.HTML作用 把网页的信息格式化的展现,对网页信息进行规范化展示 连接(https ...

  10. 通过SSE(Server-Send Event)实现服务器主动向浏览器端推送消息

    一.SSE介绍 1.EventSource 对象 SSE 的客户端 API 部署在EventSource对象上.下面的代码可以检测浏览器是否支持 SSE. if ('EventSource' in w ...