当前位置:网站首页>【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)。
边栏推荐
- numpy.isclose
- Is TCP reliable?Why?
- 【C语言进阶】文件操作(二)
- Quartus uses tcl files to quickly configure pins
- npm npm
- 6132. All the elements in the array is equal to zero - quick sort method
- cdh的hue上oozie启动报错,Cannot allocate containers as requested resource is greater than maximum allowed
- chrome copies the base64 data of an image
- numpy.where
- solidity
猜你喜欢

ICLR 2022最佳论文:基于对比消歧的偏标签学习

工作5年,测试用例都设计不好?来看看大厂的用例设计总结

nodejs--process

Get piggy homestay (short-term rental) data

Secondary Vocational Network Security Competition B7 Competition Deployment Process

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

一款简洁的文件传输工具

Share an interface test project (very worth practicing)

很多人喜欢用多御安全浏览器,竟是因为这些原因

Appears in oozie on CDH's hue, error submitting Coordinator My Schedule
随机推荐
如何更好的理解的和做好工作?
2022第六届强网杯部分wp
[LeetCode304 Weekly Competition] Two questions about the base ring tree 6134. Find the closest node to the given two nodes, 6135. The longest cycle in the graph
Flink Yarn Per Job - CliFrontend
Architecture basic concept and nature of architecture
cdh6打开oozieWeb页面,Oozie web console is disabled.
Secondary Vocational Network Security Competition B7 Competition Deployment Process
云原生DevOps环境搭建
Quartus uses tcl files to quickly configure pins
一道golang中关于iota的面试题
@Scheduled注解详解
【图像融合】基于加权和金字塔实现图像融合附matlab代码
What is CICD excuse me
数据机构---第五章树与二叉树---二叉树的概念---应用题
Getting started with IDEA is enough to read this article
分享一份接口测试项目(非常值得练手)
6134. Find the closest node to the given two nodes - force double hundred code
检查 Oracle 版本的 7 种方法
根本上解决mysql启动失败问题Job for mysqld.service failed because the control process exited with error code
Flink Yarn Per Job - Yarn应用