Let's say a positive integer is a superpalindrome if it is a palindrome, and it is also the square of a palindrome.html
Now, given two positive integers L
and R
(represented as strings), return the number of superpalindromes in the inclusive range [L, R]
.git
Example 1:github
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 <= len(L) <= 18
1 <= len(R) <= 18
L
and R
are strings representing integers in the range [1, 10^18)
.int(L) <= int(R)
这道题对于正整数定义了一种超级回文数,即其自己是回文数,而且是另外一个回文数的平方,如今给了咱们一个范围 [L, R],让返回这个范围内全部超级回文数的个数。固然最暴力的办法就是遍历每一个数字,而后看其是不是回文数字,而后再检测其平方数是不是回文数,这种方法基本不太可能经过 OJ,由于绝大多的数字都不是回文数,每一个都检测一遍实在是太费时间了,那么应该怎么办呢?实际上咱们应该主动的去构建回文数,对于一个回文数字,若在两边各加上一个相同的数字,则新组成的数字必定也是回文数字,那么对于这个新组成的回文数,只须要验证一下其平方数是否也是回文数便可,这样就大大的减小了运算步骤,从而逃脱 OJ 的魔爪。ui
具体来讲,因为给定的L和R范围超过了整型最大值,因此要转为长整型。而后须要使用上面提到的方法来构建回文数,因为回文数有奇数和偶数两种形式,好比 22 就是偶数形式,131 就是奇数形式。先构造偶数个的回文数,就是直接在空串的两端加相同的数字便可,构建的过程放到一个递归函数中。同理,构造奇数个的回文数,就是先生成中间的单独数字,这里能够是从0到9的任意数字,而后再在两边加相同的数字,调用递归函数。在递归函数,首先判断当前数字的长度,若超过了9,说明当前数字的平方数长度会超过 18,须要直接返回,由于题目中限定了L和R的长度不超过 18。再判断若当前数字不为空,且第一个数字不为0时,要验证其平方数是否为回文数。由于多位数的首位不能是0,题目中给定了L和R的范围是从1开始的,因此不会是单独的0。此时咱们将当前数字的字符串转为长整型,而后计算其平方数,若该数字大于右边界 right,则直接返回,不然看若数字大于等于左边界,且是回文数的话,结果 res 自增1。以后就要继续构建新的回文数,作法仍是在两边同时增长两个相同的数字,并对每一个新构建的回文数调用递归便可,参见代码以下:code
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'; ++c) { helper(string(1, c), left, right, res); } return res; } void helper(string cur, long& left, long& right, int& res) { if (cur.size() > 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 <= '9'; ++c) { helper(string(1, c) + cur + string(1, c), left, right, res); } } bool isPalindrome(string str) { int left = 0, right = (int)str.size() - 1; while (left < right) { if (str[left++] != str[right--]) return false; } return true; } };
Github 同步地址:htm
https://github.com/grandyang/leetcode/issues/906blog
参考资料:递归
https://leetcode.com/problems/super-palindromes/ci
https://leetcode.com/problems/super-palindromes/discuss/170774/Java-building-the-next-palindrome