当前位置:网站首页>【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)。
边栏推荐
猜你喜欢
在MySQL中使用MD5加密【入门体验】
云原生DevOps环境搭建
The third chapter of the imitation cattle network project: develop the core functions of the community (detailed steps and ideas)
ICLR 2022 Best Paper: Partial Label Learning Based on Contrastive Disambiguation
检查 Oracle 版本的 7 种方法
Architecture basic concept and nature of architecture
Docker实践经验:Docker 上部署 mysql8 主从复制
Building a cloud-native DevOps environment
Access the selected node in the console
Share an interface test project (very worth practicing)
随机推荐
6134. Find the closest node to the given two nodes - force double hundred code
What is CICD excuse me
JAX-based activation function, softmax function and cross entropy function
Docker实践经验:Docker 上部署 mysql8 主从复制
带你搞懂MySQL隔离级别,两个事务同时操作同一行数据会怎样?
程序员还差对象?new一个就行了
Is TCP reliable?Why?
Get piggy homestay (short-term rental) data
【MySQL系列】MySQL数据库基础
Access the selected node in the console
cdh6 opens oozieWeb page, Oozie web console is disabled.
Convert LocalDateTime to Date type
Spark Sql之union
洞见云原生微服务及微服务架构浅析
机器学习文本分类
怎样做才能让这条SQL变成一条危险的SQL?
numpy.isclose
thinkphp漏洞总结
Deep Learning Fundamentals - Numpy-based Recurrent Neural Network (RNN) implementation and backpropagation training
Building a cloud-native DevOps environment