Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[LeetCode] 906. Super Palindromes #906

Open
grandyang opened this issue May 30, 2019 · 0 comments
Open

[LeetCode] 906. Super Palindromes #906

grandyang opened this issue May 30, 2019 · 0 comments

Comments

@grandyang
Copy link
Owner

grandyang commented May 30, 2019

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'; ++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 同步地址:

#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 题目讲解汇总(持续更新中...)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant