- 921. Minimum Add to Make Parentheses Valid 使括号有效的最少添加
- 915. Partition Array into Disjoint Intervals 分割数组
- 932. Beautiful Array 漂亮数组
- 940. Distinct Subsequences II 不同的子序列 II
题目地址: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
andR
arestringsrepresentingintegersintherange[1,10^18)
.;
4、 int(L)<=int(R)
;
找出在[L, R]
双闭区间内,所有的超级回文数个数。超级回文数是指本身是回文数,并且它的算数平方根也是回文数。
暴力求解的话一定超时,很明显超级回文数是很稀少的,我们还是想想怎么能找规律吧。这是一些超级回文数:
# 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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
https://leetcode.com/problems/super-palindromes/discuss/171450/Python-AC-bfs-detail-explanation
DDKK.COM 弟弟快看-教程,程序员编程资料站,版权归原作者所有
本文经作者:负雪明烛 授权发布,任何组织或个人未经作者授权不得转发
问题比较Java,但是我想在Android中实现: 假设有 3 个或更多类相互扩展: class A { ... int color; ... } class B extend
我知道标题听起来有点奇怪,但这正是我愿意做的。简单解释:A类是B类的子类,B类也是C类的子类>. 现在,所有这些类都包含方法m()。在我的 A 类中,这是我唯一可以访问的类,因为其他类仅在运行时可用,
我有一个 UIViewController 类 A 和 B。 A 使用以下方式加载 B:[A.view addSubView B.view]。 B 有一个带有“后退”按钮的导航栏。我想在单击时返回到
我有以下(第三方)类结构。我们将调用第三方项目 ProjectSeriously,并注意我使用 System.out.println 代替其他复杂的功能(100 行代码) . class A {
在下面的代码中,我从 Game 扩展了 MyGame。我有两个问题: 我们是否需要为所有render()、dispose()、pause()调用super方法 和 resize(w,h)?很多人都没有
例如,假设我想在调用 super.viewDidLoad() 时跳过一级。所以我希望能够做这样的事情: override func viewDidLoad() { super.super.vi
public class Faculty extends Employee { public static void main(String[] args) { new Fac
假设我有: class Superclass { //fields... methodA() {...} methodB() {...} ... } class Subclass exte
这个问题在这里已经有了答案: Why is super.super.method(); not allowed in Java? (22 个答案) 关闭 9 年前。 我怀疑我想做的事情是否可行。我有
我有一个实现 Initializable 的类。 public abstract class ExampleClass implements Initializable { public vo
我想知道,我有这个大数组,是否可以只在内存中使用一次而不是每个线程一次?以 stackoverflow 上的标签为例。他们几乎从不改变,为什么不为他们留下一个内存点呢?甚至可能将该数组永久保存在内存中
假设这三个类具有这个简单的层次结构: class A { func foo() { print("A") } } class B: A { override fu
有没有办法在 TypeScript 中调用 super.super.methodName。我想避免调用super.methodName,但我想调用二祖的methodName方法。 谢谢。 最佳答案 T
这个问题已经有答案了: When do I use super()? (11 个回答) 已关闭 7 年前。 package Geometry; public abstract class Geomet
我必须执行and()在我的实现 Predicate 的业务对象上. 出现问题的代码是 and() 行调用: Predicate predicate = new M
我有一个实现接口(interface)的抽象父类(super class): public abstract class FooMatrix implements Matrix { publi
我有四个 UIView:viewA 是 Root View ,它有 viewB 作为它的 subview 。 viewB 将 viewC 作为其 subview ,而 viewC 将 viewD 作为
有什么区别: class Child(SomeBaseClass): def __init__(self): super(Child, self).__init__() 和:
我有一个通用接口(interface) interface ListList extends List> .由于某些原因,我无法转换 ListList至 List> .有什么方法可以做到吗?为什么它不
我想调用带有两个参数的父类(super class)的构造函数,所以我调用了 super(arguments),但是编译器说: “类 Person 中的构造函数 Person 不能应用于给定类型; 要
我是一名优秀的程序员,十分优秀!