当前位置:网站首页>【Leetcode】479. Largest Palindrome Product
【Leetcode】479. Largest Palindrome Product
2022-08-01 23:47:00 【记录算法题解】
题目地址:
https://leetcode.com/problems/largest-palindrome-product/
给定一个正整数 1 ≤ n ≤ 8 1\le n\le 8 1≤n≤8,求最大的回文数 x x x使得 x x x能分解为两个 n n n位数的乘积。
直接暴力枚举即可。代码如下:
class Solution {
public:
int largestPalindrome(int n) {
if (n == 1) return 9;
int maxv = pow(10, n) - 1;
for (int i = maxv;; i--) {
auto a = to_string(i);
auto b = a;
reverse(b.begin(), b.end());
auto num = stoll(a + b);
for (long j = maxv; j * j >= num; j--)
if (num % j == 0) return num % 1337;
}
return 0;
}
};
时间复杂度 O ( 1 0 n ) O(10^n) O(10n),空间 O ( n ) O(n) O(n)。
边栏推荐
- Quartus 使用 tcl 文件快速配置管脚
- 工件SSMwar exploded 部署工件时出错。请参阅服务器日志了解详细信息
- 工作5年,测试用例都设计不好?来看看大厂的用例设计总结
- nodejs--process
- Classical Literature Reading--DLO
- 机器学习文本分类
- Data Organization --- Chapter 5 Trees and Binary Trees --- The Concept of Binary Trees --- Application Questions
- 获取小猪民宿(短租)数据
- ansible模块--copy模块
- WEB安全基础 - - - XRAY使用
猜你喜欢

C language - branch statement and loop statement

Making a Simple 3D Renderer

多御安全浏览器android版更新至1.7,改进加密协议

在MySQL登录时出现Access denied for user ‘root‘@‘localhost‘ (using password YES) 拒绝访问问题解决

字节跳动面试官:请你实现一个大文件上传和断点续传

Data Organization --- Chapter 5 Trees and Binary Trees --- The Concept of Binary Trees --- Application Questions

@WebServlet注解(Servlet注解)

在CDH的hue上的oozie出现,提交 Coordinator My Schedule 时出错

【图像融合】基于加权和金字塔实现图像融合附matlab代码

Work for 5 years, test case design is bad?To look at the big case design summary
随机推荐
Thinkphp 5.0.24变量覆盖漏洞导致RCE分析
2022第六届强网杯部分wp
Chapter 11 Working with Dates and Times
WEB安全基础 - - - XRAY使用
获取小猪民宿(短租)数据
What is CICD excuse me
Flink Yarn Per Job - 提交流程一
@WebServlet注解(Servlet注解)
cdh6打开oozieWeb页面,Oozie web console is disabled.
Quartus 使用 tcl 文件快速配置管脚
技术分享 | 接口测试中如何使用Json 来进行数据交互 ?
color transparency parameter
ansible模块--copy模块
[C language advanced] file operation (2)
asyncawait和promise的区别
An interview question about iota in golang
Calculate the midpoint between two points
深度学习基础-基于Numpy的循环神经网络(RNN)实现和反向传播训练
C language - branch statement and loop statement
Programmer is still short of objects? A new one is enough