Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 9a331c0

Browse files
✨feat: Add 479
1 parent 8190e95 commit 9a331c0

File tree

2 files changed

+78
-0
lines changed

2 files changed

+78
-0
lines changed

‎Index/数学.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
| [458. 可怜的小猪](https://leetcode-cn.com/problems/poor-pigs/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/poor-pigs/solution/gong-shui-san-xie-jin-zhi-cai-xiang-xian-69fl/) | 困难 | 🤩🤩🤩🤩 |
3434
| [470. 用 Rand7() 实现 Rand10()](https://leetcode-cn.com/problems/implement-rand10-using-rand7/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/implement-rand10-using-rand7/solution/gong-shui-san-xie-k-jin-zhi-zhu-wei-shen-zmd4/) | 中等 | 🤩🤩🤩🤩 |
3535
| [477. 汉明距离总和](https://leetcode-cn.com/problems/total-hamming-distance/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/total-hamming-distance/solution/gong-shui-san-xie-ying-yong-cheng-fa-yua-g21t/) | 简单 | 🤩🤩🤩 |
36+
| [479. 最大回文数乘积](https://leetcode-cn.com/problems/largest-palindrome-product/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/largest-palindrome-product/solution/by-ac_oier-t8j7/) | 困难 | 🤩🤩🤩 |
3637
| [483. 最小好进制](https://leetcode-cn.com/problems/smallest-good-base/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/smallest-good-base/solution/gong-shui-san-xie-xiang-jie-ru-he-fen-xi-r94g/) | 困难 | 🤩🤩🤩🤩 |
3738
| [507. 完美数](https://leetcode-cn.com/problems/perfect-number/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/perfect-number/solution/gong-shui-san-xie-jian-dan-mo-ni-tong-ji-e6jk/) | 简单 | 🤩🤩🤩 |
3839
| [523. 连续的子数组和](https://leetcode-cn.com/problems/continuous-subarray-sum/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/continuous-subarray-sum/solution/gong-shui-san-xie-tuo-zhan-wei-qiu-fang-1juse/) | 中等 | 🤩🤩🤩🤩 |
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
### 题目描述
2+
3+
这是 LeetCode 上的 **[479. 最大回文数乘积](https://leetcode-cn.com/problems/largest-palindrome-product/solution/by-ac_oier-t8j7/)** ,难度为 **困难**
4+
5+
Tag : 「枚举」、「数学」
6+
7+
8+
9+
给定一个整数 $n$ ,返回 可表示为两个 $n$ 位整数乘积的 最大回文整数 。
10+
11+
因为答案可能非常大,所以返回它对 1337ドル$ 取余 。
12+
13+
示例 1:
14+
```
15+
输入:n = 2
16+
17+
输出:987
18+
19+
解释:99 x 91 = 9009, 9009 % 1337 = 987
20+
```
21+
示例 2:
22+
```
23+
输入: n = 1
24+
25+
输出: 9
26+
```
27+
28+
提示:
29+
* 1ドル <= n <= 8$
30+
31+
---
32+
33+
### 枚举 + 数学
34+
35+
对于数位为 $n$ 的两个数而言,其乘积的位数要么是 2ドル * n,ドル要么是 2ドル * n - 1$。
36+
37+
当数位 $n > 1$ 时,我们总能在数位为 2ドル * n$ 中找到答案。
38+
39+
利用回文串的特性,我们只需枚举回文串的前半部分即可(后半部分唯一确定),我们只要在枚举前半部分时按照「从大到小」进行,即可确保找到的第一个合法值为最大数,对于一个数位为 $n$ 的最大数为 10ドル^n - 1$。
40+
41+
具体的,当枚举到回文串的前半部分 $i$ 时,我们利用回文串特性构造出具实际的回文数值 $nums,ドル随后检查 $nums$ 能否分解成数位为 $n$ 的数对 $(a, b),ドル利用乘法具有交换律,我们只需要枚举数对中的较大数即可。
42+
43+
代码:
44+
```Java
45+
class Solution {
46+
public int largestPalindrome(int n) {
47+
if (n == 1) return 9;
48+
int max = (int) Math.pow(10, n) - 1;
49+
for (int i = max; i >= 0; i--) {
50+
long num = i, t = i;
51+
while (t != 0) {
52+
num = num * 10 + (t % 10);
53+
t /= 10;
54+
}
55+
for (long j = max; j * j >= num; j--) {
56+
if (num % j == 0) return (int)(num % 1337);
57+
}
58+
}
59+
return -1;
60+
}
61+
}
62+
```
63+
* 时间复杂度:枚举回文串的前半部分复杂度为 $O(10^n)$;检查回文串能否被分解复杂度为 $O(10^n)$。整体复杂度为 $O(10^{2n})$
64+
* 空间复杂度:$O(1)$
65+
66+
---
67+
68+
### 最后
69+
70+
这是我们「刷穿 LeetCode」系列文章的第 `No.479` 篇,系列开始于 2021年01月01日,截止于起始日 LeetCode 上共有 1916 道题目,部分是有锁题,我们将先把所有不带锁的题目刷完。
71+
72+
在这个系列文章里面,除了讲解解题思路以外,还会尽可能给出最为简洁的代码。如果涉及通解还会相应的代码模板。
73+
74+
为了方便各位同学能够电脑上进行调试和提交代码,我建立了相关的仓库:https://github.com/SharingSource/LogicStack-LeetCode
75+
76+
在仓库地址里,你可以看到系列文章的题解链接、系列文章的相应代码、LeetCode 原题链接和其他优选题解。
77+

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /