[Leetcode] 479. Largest Palindrome Product 解题报告

[南大ICS]PA1 - 开天辟地的篇章: 最简单的计算机 task PA1.1: 实现单步执行, 打印寄存器状态, 扫描内存 task PA1.2: 实现算术表达式求值 task PA1.3: 实现所有要求, 提交完整的实验报告 阅读详情

题目:

Find the largest palindrome made from the product of two n-digit numbers.

Since the result could be very large, you should return the largest palindrome mod 1337.

Example:

Input: 2

Output: 987

Explanation: 99 x 91 = 9009, 9009 % 1337 = 987

Note:

The range of n is [1,8].

思路:

1、查表法:既然题目中说了n的范围是[1, 8],那么我们提前计算出8个结果存放在表中,在函数中直接返回即可。还有谁比我更流氓?

2、常规法:注意到n == 1的时候,最大回文乘积是1位数;当n >=2的时候,最大回文乘积都是2n位数。那么我们就可以从大到小枚举位数为2n的回文数,一旦发现它是两个n位数的乘积,就返回。注意在下面的代码片段中,我们尝试j是不是可以整除cand,如果可以,就直接返回了,而并没有检查cand / j是否在[lower, upper]的范围之内。这是为什么呢?1)证明cand / j >= lower。我们看看cand / j的最小值是什么时候产生的?它应该是cand取最小值,j取最大值的时候产生的,但即使cand取最小值,它也是2n位数,j取最大值的时候是9...9(一共n位),lower一定是10...(一共n位),那么j * lower一定是2n - 1位,所以一定小于cand,所以cand / j > lower。2)cand / j在什么时候取最大值呢?显然是j最小的时候,即sqrt(cand),也就是说cand / j的最大值是sqrt(cand)。一个2n位数的平方根一定是n位数,这是因为最小的n+1位数的平方也会达到2n+1位。所以,sqrt(cand) <= upper的(因为upper是最大的n位数)。

代码:

1、查表法:

class Solution {
public:
    int largestPalindrome(int n) {
        long long ret[8] = {9, 9009, 906609, 99000099, 9966006699, 999000000999, 99956644665999, 9999000000009999};
        return ret[n - 1] % 1337;
    }
};

2、常规法:

class Solution {
public:
    int largestPalindrome(int n) {
        if (n == 1) {
            return 9;
        }
        int upper = pow(10, n) - 1, lower = pow(10, n - 1);     // the range of the digits with n-bits
        for (int i = upper; i >= lower; --i) {                  // i + reverse(i) may form the palindrome
            long cand = buildPalindrome(i);
            for (long j = upper; j * j >= cand; --j) {
                if (cand % j == 0) {
                    return cand % 1337;
                }
            }
        }
        return -1;
    }
private:
    long buildPalindrome(int n) {
        string s = to_string(n);
        reverse(s.begin(), s.end());
        return stol(to_string(n) + s);
    }
};
[RK-Linux] 移植Linux-5.10到RK3399(十)| 配置AP6256模组使能WIFI、BT功能 阅读详情

相关推荐

Dinic算法的复杂度分析及证明

一般图 复杂度主要取决于dfs增广的过程。 将dfs部分的复杂度分成两部分来分析:1)修改增广路上边的流量。至多会增广mmm次,一条增广路的长度至多是nnn,所以这一部分的复杂度是O(nm)O(nm)O(nm)。2)dfs遍历时找增广路失败时经过的边。由于一旦从某条边出发找最短路失败了,我们就不会再走那条边(当前弧优化),所以这一部分的复杂度是O(m)O(m)O(m)的。故而dfs增广的复杂度是O...

这个博客已经搬到了:zhongyuwei.github.io 4397

leetcode 479 最大回文数

题目 你需要找到由两个 n 位数的乘积组成的最大回文数。 由于结果会很大,你只需返回最大回文数 mod 1337得到的结果。 示例 输入: 2 输出: 987 解释: 99 x 91 = 9009, 9009 % 1337 = 987 说明 n 的取值范围为 [1,8]。 解题思路 当n==1时,最大的回文数是9,为奇数,单独做特殊情况处理; 当n为其他值时,...

xiangxianghehe的博客 1703

SolidWorks+ADAMS双验证:你的机械臂力矩计算真的靠谱吗?

本文探讨了机械臂力矩计算中手工计算的局限性,并介绍了SolidWorks和ADAMS双验证方法的重要性。通过实际案例展示了动态工况下传统计算方法的误差,详细解析了SolidWorks精准质心捕捉和ADAMS动力学仿真的关键技术,帮助工程师构建计算-仿真-实测闭环验证流程,确保机械臂设计的可靠性和安全性。

grafana6viz的博客 234

Leetcode479. 找到两个位数相同的数相乘的最大回文积

最大回文积

obrcnh的博客 1472

SAP ABAP ALV表格实战:5分钟搞定单元格下拉框配置(附完整代码)

本文详细介绍了在SAP ABAP开发中如何快速配置ALV表格的单元格下拉框功能,通过5分钟实战教程帮助开发者掌握关键步骤。文章包含完整的ABAP代码示例,涵盖下拉值定义、字段目录配置及ALV控件绑定等核心操作,特别适合需要实现财务系统费用类型选择或采购系统物料分类等场景的开发者参考。

weixin_28331555的博客 113

CAPL编程入门:从零开始掌握汽车通信脚本语言

本文详细介绍了CAPL编程的基础知识,从开发环境搭建到核心语法精要,再到总线通信实战和高级调试技巧。CAPL作为汽车通信脚本语言,专为CAN、LIN等网络协议设计,帮助工程师高效完成车载网络测试。通过实战案例和性能优化建议,读者可以快速掌握CAPL编程技能。

s1t2u3的博客 989

LeetCode: 479. Largest Palindrome Product

Find the largest palindrome made from the product of two n-digit numbers. Since the result could be very large, you should return the largest palindrome mod 1337.Example: Input: 2 Output: 987 Explanat

u010370157的博客 1406

RK3568笔记五十四:SORT实时目标追踪

YOLOv5具备目标检测的功能,把视频分解成多幅图像并逐帧执行时,如果视频帧中有多个目标,如何知道一帧中的目标和上一帧是同一个对象就是目标跟踪的工作。DeepSort是实现目标跟踪的算法,从sort(simple online and realtime tracking)演变而来,使用卡尔曼滤波器预测所检测对象的运动轨迹,匈牙利算法将它们与新的检测目标相匹配。结合YOLOv5和DeepSORT可以在实时场景中实现高效的目标检测和跟踪。

800

leetcode 479. Largest Palindrome Product最大的回文乘积

leetcode 479. Largest Palindrome Product最大的回文乘积

Shauna_Wu的博客 2284

华为OD机试真题 新系统 - 8位LED控制器 (Java/Py/C/C++/Js/Go)

本文整理华为 OD 机试新系统机考真题库,涵盖高频考点精析与完整题目内容,提供 C 语言、Java、C++、Python、Go、JavaScript 多语言源码实现,搭配详细解题思路讲解,并支持在线 OJ 刷题练习,助力高效备考华为 OD 机试新系统。

算法大师! 1989

在 SAP S/4HANA Public Cloud 中,用 RAP 一键生成 PDF 并在 Fiori 前端触发下载的完整方案

create;update;delete;// 生成并返回当前文档上下文。

2007 年 ~ 2025 年,深耕 SAP 技术 18 年 425

【leetcode】479. Largest Palindrome Product(Python & C++)

479. Largest Palindrome Product题目链接479.1 题目描述:Find the largest palindrome made from the product of two n-digit numbers.Since the result could be very large, you should return the largest palindrome mod

I am what i am 1156

深度探索:VS2022 牵手 DeepSeek,开启开发新征程

通过将 VS2022 与 DeepSeek 接入,我们成功开启了智能开发的新征程。从前期的硬件准备、软件环境搭建,到实战环节的一步步配置与整合,再到全面的测试与验证,每一个步骤都充满挑战,但也带来了满满的收获。在实际应用中,VS2022 与 DeepSeek 的结合展现出了强大的优势,为开发者带来了更高的效率和更出色的代码质量。无论是代码自动补全、智能代码审查,还是自然语言交互编程,都让开发过程变得更加智能、便捷,极大地提升了开发体验。

远程部署调试 运行安装 项目调试 二次开发 项目技术新 持续迭代 部分源码免费分享 3536

【LeetCode】479. Largest Palindrome Product 解题报告(Python & C++)

【LeetCode】479. Largest Palindrome Product 解题报告(Python) 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/largest-palindrome-product/description/ 题目描述: Find the largest palindrome made from...

负雪明烛 1303

RU DOS版、EFI版32bit/64bit BIOS常用工具

DOS版、EFI版的RU工具,可用来查看IO、MEM、PCI、SMBIOS等资源。亲测在EFI SHELL下可用。

InTheHand.Net.Personal+BlueTooth4,蓝牙开发

项目中有蓝牙开发的必备dll,现在网上相关的文档博客不少,有的说的多有的说少,但是却很少有这两个dll,分享一下。

LeetCode //C - 479. Largest Palindrome Product

【代码】LeetCode //C - 479. Largest Palindrome Product。

Made in Code 596

visio安装包

Microsoft Office Visio 是一款专业的制图软件。利用经过更新的形状、协作工具和数据链接图表,创建专业级图表以简化复杂的信息。

leetcode 479. Largest Palindrome Product

479. Largest Palindrome Product 注意数值类型!(int) (pro%m) public class Solution{ public static void main(String[] args){ Solution s = new Solution(); s.largestPalindrome(2); } public

独钓寒江雪 2372

ET2012新版免狗免安装绿色软件

ET2012新版免狗免安祖昂绿色软件。,新界面,新视觉,

{LeetCode} 479. Largest Palindrome Product

自己写的代码,没过AC,原因是 Submission Result: Time Limit Exceeded class Solution { public: int largestPalindrome(int n) { double max = 0; for (int i=0; i<n; i++) { max

sheldon0227的博客 2328

LeetCode479 Largest Palindrome Product

Find the largest palindrome made from the product of two n-digit numbers. Since the result could be very large, you should return the largest palindrome mod 1337. Example: Input: 2 Output: 987 ...

李歇特冯-兹拜因巴哈的博客 221

leetcode 479. 最大回文乘积 Largest Palindrome Product 解法 python

一.问题描述 你需要找到由两个 n 位数的乘积组成的最大回文数。 由于结果会很大,你只需返回最大回文数 mod 1337得到的结果。 示例: 输入: 2 输出: 987 解释: 99 x 91 = 9009, 9009 % 1337 = 987 说明: n 的取值范围为[1,8]。 Find the largest palindrome made from the produc...

CSerwangjun的博客 433

LeetCode 479 largest-palindrome-product

LeetCode 479 这个题目除了BruteForce的方法,我没有想出好的解法。最后求助于其他人的解法了,看到一个数学解法比较有意思,转成了python: def largestPalindromeMath(self, n: int) -> int: '''假设确实存在 P = X * Y, X,Y都是接近10^n的数,那么 记 X = 10^n...

frankguodongchen的博客 159

【Leetcode】479. Largest Palindrome Product(Easy)

1.题目Find the largest palindrome made from the product of two n-digit numbers.Since the result could be very large, you should return the largest palindrome mod 1337.Example:Input: 2Output: 987Explanat...

不想做菜鸟的小螃蟹 382

【回文串10】LeetCode 479. Largest Palindrome Product

LeetCode 479. Largest Palindrome Product 本博客转载自:http://www.cnblogs.com/grandyang/p/7644725.html Solution1:

Allenlzcoder的博客 272

LeetCode | 479. Largest Palindrome Product 回文数

Find the largest palindrome made from the product of two n-digitnumbers. Since the result could be very large, you should return thelargest palindrome mod 1337. Example: Input: 2 Output: 987 Expl

u012737193的博客 260

LeetCode---479. Largest Palindrome Product 生成最大回文

LeetCode—479. Largest Palindrome Product 生成最大回文 题目 输入n代表两个数字的位数,,这两个数字做乘积,使得结果是回文,并且是所有回文结果中最大的。 n取1~8,并且因为结果可能很大,所以要求输出结果对1337取模,例如 Input: 2 Output: 987 Explanation: 99 x 91 = 9009, 9009 % 133...

pnnngchg的博客 212

LeetCode 479. Largest Palindrome Product

Find the largest palindrome made from the product of two n-digit numbers. Since the result could be very large, you should return the largest palindrome mod 1337. Example: Input: 2 Output: 987 Ex

柳婼 の blog 479

LeetCode #479 - Largest Palindrome Product

题目描述: Find the largest palindrome made from the product of two n-digit numbers. Since the result could be very large, you should return the largest palindrome mod 1337. Example: Input: 2 Output: ...

LawFile的博客 248
上一篇: [Leetcode] 477. Total Hamming Distance 解题报告
下一篇: [Leetcode] 480. Sliding Window Median 解题报告
魔豆Magicbean
博客等级 码龄19年 146粉丝 742原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值