难度:简单
给定一个整数 n ,返回 可表示为两个
n位整数乘积的 最大回文整数 。因为答案可能非常大,所以返回它对1337取余 。示例 1:
输入:n = 2 输出:987 解释:99 x 91 = 9009, 9009 % 1337 = 987示例 2:
输入:n = 1 输出:9提示:
1 <= n <= 8
题解:
pythonclass Solution: def largestPalindrome(self, n: int) -> int: if n == 1: return 9 # 1位数的最大回文数是9 # 生成 n 位数的最大值 max_num = int("9" *n) # 例如,n=3时,max_num = 999 min_num = 10 ** (n - 1) # n位数的最小值,即100 max_palindrome = 0 # 储存找到的最大回文数 # 从最大 n 位数开始逆序遍历 for i in range(max_num, min_num-1, -1): for j in range(i, min_num-1, -1): # j 从 i 开始,以减少重复计算 product = i * j # 检查乘积是否为回文 if str(product) == str(product)[::-1]: max_palindrome = max(max_palindrome, product) # 更新最大回文数 break # 找到的最大回文数可立即使用 return max_palindrome % 1337 # 返回结果对1337取余
leetcode:479. 最大回文数乘积(python3解法,数学相关算法题)
心软且酷丶2025-05-31 12:10
相关推荐
Python图像识别6 分钟前
17-【2027毕设】YOLO11水稻病害检测识别系统 - Python完整源码+PyQt5界面+训练模型+数据集天天被压力33 分钟前
【别再到处找免费股票数据API了:官方204个接口,32篇一次讲透 #02】涨停跌停与特色股池:5类股池接口一次拉全Wx-bishekaifayuan35 分钟前
springboot甘肃特产服务平台50301-计算机课程设计、毕业设计midelshuang38 分钟前
RAG 文档解析(三):bbox 实战——多栏排版与水印 PDFFYKJ_20101 小时前
springboot雅集社区养老管理系统04456-计算机课程设计、毕业设计zhanghaha13141 小时前
Python进阶教程:35_PyQt (保姆级零基础教程)信誓旦旦的程序猿1 小时前
【Python 量化取数指南 #09】Python 拿港股通数据:港股通成交与港股财报实测FYKJ_20101 小时前
springboot羽毛球场地管理系统00626-计算机课程设计、毕业设计Wx-bishekaifayuan2 小时前
django医院营收信息预测系统49414-计算机课程设计、毕业设计Wx-bishekaifayuan2 小时前
springboot会议室预约管理系统42030-计算机课程设计、毕业设计