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)

Approach #1: Math. [Java]

class Solution {
public int superpalindromesInRange(String L, String R) {
Long l = Long.valueOf(L), r = Long.valueOf(R);
int result = 0;
for (long i = (long)Math.sqrt(l); i * i <= r;) {
long p = nextP(i);
if (p * p <= r && isP(p * p)) {
result++;
}
i = p + 1;
}
return result;
} private long nextP(long l) {
String s = Long.toString(l);
int N = s.length();
String half = s.substring(0, (N + 1) / 2);
String reverse = new StringBuilder(half.substring(0, N/2)).reverse().toString();
long first = Long.valueOf(half + reverse);
if (first >= l) return first;
String nextHalf = Long.toString(Long.valueOf(half) + 1);
String reverseNextHalf = new StringBuilder(nextHalf.substring(0, N/2)).reverse().toString();
long second = Long.valueOf(nextHalf + reverseNextHalf);
return second;
} private boolean isP(long l) {
String s = "" + l;
int i = 0, j = s.length() - 1;
while (i < j) {
if (s.charAt(i++) != s.charAt(j--)) {
return false;
}
}
return true;
}
}

  

Reference:

Calculating the sqrt of nums from [L, R] (denote with 's')

finding the front half of 's' (denote with 'half')

the combination of the front half and its reverse (denote with 'p')

if p * p >= l and p * p <= r:

  judge p * p is palindromes or not

else :

  Calculating the combination of the front half + 1 and its reverse (denote with 'p')   

Reference:

https://leetcode.com/problems/super-palindromes/discuss/170774/Java-building-the-next-palindrome

906. Super Palindromes的更多相关文章

  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】906. Super Palindromes 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 BFS解法 相似题目 参考资料 日期 题目地址:ht ...

  3. [Swift]LeetCode906. 超级回文数 | Super Palindromes

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

  4. All LeetCode Questions List 题目汇总

    All LeetCode Questions List(Part of Answers, still updating) 题目汇总及部分答案(持续更新中) Leetcode problems clas ...

  5. leetcode hard

    # Title Solution Acceptance Difficulty Frequency     4 Median of Two Sorted Arrays       27.2% Hard ...

  6. Swift LeetCode 目录 | Catalog

    请点击页面左上角 -> Fork me on Github 或直接访问本项目Github地址:LeetCode Solution by Swift    说明:题目中含有$符号则为付费题目. 如 ...

  7. URAL 2040 Palindromes and Super Abilities 2 (回文自动机)

    Palindromes and Super Abilities 2 题目链接: http://acm.hust.edu.cn/vjudge/contest/126823#problem/E Descr ...

  8. Ural 2040. Palindromes and Super Abilities 2 回文自动机

    2040. Palindromes and Super Abilities 2 题目连接: http://acm.timus.ru/problem.aspx?space=1&num=2040 ...

  9. 回文树(回文自动机) - URAL 1960 Palindromes and Super Abilities

     Palindromes and Super Abilities Problem's Link: http://acm.timus.ru/problem.aspx?space=1&num=19 ...

随机推荐

  1. join为啥会阻塞主线程?

    join使用 上篇我们介绍了CountDownLatch,顺便说到了Thread中的join方法! import java.util.concurrent.TimeUnit; /** * @autho ...

  2. secure 审计暴力登陆

    文件路径 cd /var/log -rw------- 1 root root 1200063 Aug 10 20:04 secure 做应急响应,或者做脚本监控的时候,都可以参考如下特征 ... A ...

  3. 【Arduino学习笔记06】上拉电阻和下拉电阻

    为什么要用上拉电阻和下拉电阻?--避免输入引脚处于"悬空"状态 下图是一个没有使用上拉电阻/下拉电阻的电路图: 在按键没有按下时,要读取的输入引脚没有连接到任何东西,这种状态就称为 ...

  4. 【Arduino学习笔记04】消抖动的按键切换

    "开关抖动": 由于按键是基于弹簧-阻尼系统的机械部件,所以当按下一个按键时,读到的信号并不是从低到高,而是在高低电平之间跳动几毫秒之后才最终稳定. 代码解读: 1 const i ...

  5. 在scanf函数中占位符使用错误而产生的一些错误

    出现的问题 在做编程题的的时候,遇到了一个很奇怪的错误,出问题的代码如下: 1 #include <cstdio> 2 using namespace std; 3 4 int main( ...

  6. 死磕Spring之IoC篇 - @Autowired 等注解的实现原理

    该系列文章是本人在学习 Spring 的过程中总结下来的,里面涉及到相关源码,可能对读者不太友好,请结合我的源码注释 Spring 源码分析 GitHub 地址 进行阅读 Spring 版本:5.1. ...

  7. 漫漫Java路1—基础知识2—注释和命名规则

    ## 注释 1. 单行注释 ```java //这是一个注释 ``` 2. 多行注释 ```java /* 这是一个注释 */ ``` 3. 文档注释 ```java /** * * * */ ``` ...

  8. MySQL全面瓦解25:构建高性能索引(案例分析篇)

    回顾一下上面几篇索引相关的文章: MySQL全面瓦解22:索引的介绍和原理分析 MySQL全面瓦解23:MySQL索引实现和使用 MySQL全面瓦解24:构建高性能索引(策略篇) 索引的十大原则 1. ...

  9. DataFocus小学堂|客户分析之复活客户分析

    复活客户分析 什么是"复活客户"?如何进行"复活客户分析"呢?今天,我们借助DataFocus系统,来了解一种简单的复活客户分析. 1.何为复活客户 复活客户, ...

  10. Python-celery的使用

    安装:pip install celery pip install eventlet 需要提前安装redis. (Download, extract and compile Redis with: $ ...