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],让返回这个范围内所有超级回文数的个数。当然最暴力的办法就是遍历每个数字,然后看其是否是回文数字,然后再检测其平方数是否是回文数,这种方法基本不太可能通过 OJ,因为绝大多的数字都不是回文数,每个都检测一遍实在是太费时间了,那么应该怎么办呢?实际上我们应该主动的去构建回文数,对于一个回文数字,若在两边各加上一个相同的数字,则新组成的数字一定也是回文数字,那么对于这个新组成的回文数,只需要验证一下其平方数是否也是回文数即可,这样就大大的减少了运算步骤,从而逃脱 OJ 的魔爪。

具体来说,由于给定的L和R范围超过了整型最大值,所以要转为长整型。然后需要使用上面提到的方法来构建回文数,由于回文数有奇数和偶数两种形式,比如 22 就是偶数形式,131 就是奇数形式。先构造偶数个的回文数,就是直接在空串的两端加相同的数字即可,构建的过程放到一个递归函数中。同理,构造奇数个的回文数,就是先生成中间的单独数字,这里可以是从0到9的任意数字,然后再在两边加相同的数字,调用递归函数。在递归函数,首先判断当前数字的长度,若超过了9,说明当前数字的平方数长度会超过 18,需要直接返回,因为题目中限定了L和R的长度不超过 18。再判断若当前数字不为空,且第一个数字不为0时,要验证其平方数是否为回文数。因为多位数的首位不能是0,题目中给定了L和R的范围是从1开始的,所以不会是单独的0。此时我们将当前数字的字符串转为长整型,然后计算其平方数,若该数字大于右边界 right,则直接返回,否则看若数字大于等于左边界,且是回文数的话,结果 res 自增1。之后就要继续构建新的回文数,做法还是在两边同时增加两个相同的数字,并对每个新构建的回文数调用递归即可,参见代码如下:

```
class Solution {
public:
int superpalindromesInRange(string L, string R) {
int res = 0;
long left = stol(L), right = stol(R);
helper("", left, right, res);
for (char c = '0'; c 9) return;
if (!cur.empty() && cur[0] != '0') {
long num = stol(cur);
num *= num;
if (num > right) return;
if (num >= left && isPalindrome(to_string(num))) ++res;
}
for (char c = '0'; c
Github 同步地址:

https://github.com/grandyang/leetcode/issues/906

参考资料:

https://leetcode.com/problems/super-palindromes/

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

https://leetcode.com/problems/super-palindromes/discuss/171467/c%2B%2B-straightforward-backtracking-solution

https://leetcode.com/problems/super-palindromes/discuss/170779/Java-Concise-DFS-Solution-with-Explaination-No-Cheating

[LeetCode All in One 题目讲解汇总(持续更新中...)](https://www.cnblogs.com/grandyang/p/4606334.html)

[LeetCode] 906. Super Palindromes 超级回文数的更多相关文章

  1. [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 ...

  2. LeetCode Problem 9:Palindrome Number回文数

    描述:Determine whether an integer is a palindrome. Do this without extra space. Some hints: Could nega ...

  3. 【leetcode算法-简单】9. 回文数

    [题目描述] 判断一个整数是否是回文数.回文数是指正序(从左向右)和倒序(从右向左)读都是一样的整数. 示例 1: 输入: 121输出: true示例 2: 输入: -121输出: false解释: ...

  4. 【LeetCode】9. Palindrome Number 回文数

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 公众号:负雪明烛 本文关键词:回文数,回文,题解,Leetcode, 力扣,Python ...

  5. LeetCode随缘刷题之回文数

    package leetcode.day_01_30; /** * 给你一个整数 x ,如果 x 是一个回文整数,返回 true :否则,返回 false . * <p> * 回文数是指正 ...

  6. LeetCode(9):回文数

    Easy! 题目描述: 判断一个整数是否是回文数.回文数是指正序(从左向右)和倒序(从右向左)读都是一样的整数. 示例 1: 输入: 121 输出: true 示例 2: 输入: -121 输出: f ...

  7. LeetCode OJ Palindrome Number(回文数)

    class Solution { public: bool isPalindrome(int x) { ,init=x; ) return true; ) return false; ){ r=r*+ ...

  8. Leetcode 564.寻找最近的回文数

    寻找最近的回文数 给定一个整数 n ,你需要找到与它最近的回文数(不包括自身). "最近的"定义为两个整数差的绝对值最小. 示例 1: 输入: "123" 输出 ...

  9. Java实现 LeetCode 564 寻找最近的回文数(今天要GG在这道题了 头晕+题难(((φ(◎ロ◎;)φ))))

    564. 寻找最近的回文数 给定一个整数 n ,你需要找到与它最近的回文数(不包括自身). "最近的"定义为两个整数差的绝对值最小. 示例 1: 输入: "123&quo ...

随机推荐

  1. python统计wav文件的时长

    import wave import os.path # 音频存放文件夹绝对路径 filedir = '/Users/111/PycharmProjects/TextClassify/wav' lis ...

  2. pywinauto教程2

    一.环境安装 1.命令行安装方法 pip install pywinauto==0.6.7 2.手动安装方法 安装包下载链接:pyWin32: python调用windows api的库https:/ ...

  3. spring context:component-scan

    <context:component-scan base-package="com.zhuguang.jack" <!-- 扫描的基本包路径 --> annota ...

  4. python合并视频

    视频合并 输入为:包含有视频的文件夹(注意路径:如   D:\\moves\\joy   双斜杠).合并后内容的名字如(我的合并视频      不用加.mp4) 输出为:我的合并视频.mp4+一个音频 ...

  5. 【LOJ#3146】[APIO2019]路灯(树套树)

    [LOJ#3146][APIO2019]路灯(树套树) 题面 LOJ 题解 考场上因为\(\text{bridge}\)某个\(\text{subtask}\)没有判\(n=1\)的情况导致我卡了\( ...

  6. linux部署java命令

    启动: #!/bin/bash source /etc/profile log() { echo `date +[%Y-%m-%d" "%H:%M:%S]` $1 } log &q ...

  7. .net core中serilog的基本使用

    Serilog的基本使用 (一)  引言 (二)  导入包 (三)  配置 直接配置 配置文件配置 (四)  使用 (五)  结语 一 引言 作为一枚小白,来复习一下serilog的使用,如果有错误的 ...

  8. 使用T4模板同时生成多个类文件

    代码: <#@ template language="C#" debug="false" hostspecific="true"#&g ...

  9. Java开发常用知识点总结

    docker exec -it imageId redis-cli docker container ls -a docker rm containerId 复制目录&文件 cp -r /ro ...

  10. HTML input属性详谈

    value属性 value属性指定输入字段的初始值: <form> 名字:<br> <input type="text" name="you ...