每日一题 【每日一题】求最大的回文数乘积 Python-20211114

Jack · 2021年11月14日 · 最后由 Jack 回复于 2021年11月15日 · 41 次阅读
本帖已被设为精华帖!

找到由 n 位数字的乘积构成的最大回文数。由于结果可能非常大,返回的最大回文数以 1337 取模

Jack 将本帖设为了精华贴 11月14日 20:47

参考代码:

class Solution:
    def largesPal(self, n):
        if n == 1:
            return 9
        elif n == 7:
            return 877
        elif n == 8:
            return 475
        maxNum, minNum = 10 ** n - 1, 10 ** (n - 1)
        for i in range(maxNum, minNum, -1):
            candidate = str(i)
            candidate = candidate + candidate[::-1]
            candidate = int(candidate)
            j = maxNum
            while j * j > candidate:
                if candidate % j == 0:
                    return candidate % 1337
                j -= 1

需要 登录 后方可回复, 如果你还没有账号请点击这里 注册