当前位置:网站首页>leetcode 866. Prime Palindrome | 866. 回文素数
leetcode 866. Prime Palindrome | 866. 回文素数
2022-07-08 00:37:00 【寒泉Hq】
题目
https://leetcode.com/problems/prime-palindrome/

题解
参考了答案:https://leetcode.com/problems/prime-palindrome/solution/
自己实现的时候,我把奇数位回文串和偶数位回文串拆开来了,最后去返回两种回文串当中的最小值,觉得这样更严谨,尽管这种严谨可以被数学证明是没有必要的。因为如果位数不同的话,结果的大小不是单调增的,不应该直接return。
例如,因为如果先算奇数,再算偶数的话,奇数是比偶数少一位的,这样有可能返回错误的最小值:
1002001 (假设它是false)
10022001(假设它是false)
1003001 (假设它是false)
10033001(假设它是true,直接返回了,是错误的最小值)
1004001 (假设它是true,这才是真正的最小值)
至于答案这样写为什么没有问题,是因为 All palindrome with even digits is multiple of 11. 相当于大于 11 的偶数都会返回 false,不会造成上述例子中返回 10033001 的情况。
class Solution {
public int primePalindrome(int n) {
if (n == 1) return 2;
long even = primePalindromeEven(n);
long odd = primePalindromeOdd(n);
return (int) Math.min(even, odd);
}
public long primePalindromeEven(int n) {
for (int L = 1; L <= 5; L++) {
// 回文root的位数L
for (int root = (int) Math.pow(10, L - 1); root < Math.pow(10, L); root++) {
// 遍历位数为L的所有回文root
// 偶数位回文串
StringBuilder sb = new StringBuilder(String.valueOf(root));
for (int i = L - 1; i >= 0; i--) {
sb.append(sb.charAt(i));
}
long num = Long.parseLong(String.valueOf(sb));
if (num >= n && isPrime(num)) {
return num;
}
}
}
return Integer.MAX_VALUE;
}
public long primePalindromeOdd(int n) {
for (int L = 1; L <= 5; L++) {
// 回文root的位数L
for (int root = (int) Math.pow(10, L - 1); root < Math.pow(10, L); root++) {
// 遍历位数为L的所有回文root
// 奇数位回文串
StringBuilder sb = new StringBuilder(String.valueOf(root));
for (int i = L - 2; i >= 0; i--) {
sb.append(sb.charAt(i));
}
long num = Long.parseLong(String.valueOf(sb));
if (num >= n && isPrime(num)) {
return num;
}
}
}
return Integer.MAX_VALUE;
}
public boolean isPrime(long n) {
if (n <= 2) return true;
for (int i = 2; i <= Math.sqrt(n); i++) {
if (n % i == 0) return false;
}
return true;
}
public static void main(String[] args) {
Solution solution = new Solution();
int i = solution.primePalindrome(9989900);
System.out.println(i);
}
}

边栏推荐
- 剑指 Offer II 041. 滑动窗口的平均值
- 2022国内十大工业级三维视觉引导企业一览
- 进程和线程的退出
- C语言-模块化-Clion(静态库,动态库)使用
- Apache multiple component vulnerability disclosure (cve-2022-32533/cve-2022-33980/cve-2021-37839)
- Uniapp one click Copy function effect demo (finishing)
- nmap工具介紹及常用命令
- GBASE观察 | 数据泄露频发 信息系统安全应如何守护
- Application of slip ring in direct drive motor rotor
- The numerical value of the number of figures thought of by the real-time update of the ranking list
猜你喜欢

神经网络与深度学习-5- 感知机-PyTorch

Get familiar with XML parsing quickly

Keras' deep learning practice -- gender classification based on inception V3

2022国内十大工业级三维视觉引导企业一览

保姆级教程:Azkaban执行jar包(带测试样例及结果)

nmap工具介绍及常用命令

pb9.0 insert ole control 错误的修复工具

云原生应用开发之 gRPC 入门

【目标跟踪】|DiMP: Learning Discriminative Model Prediction for Tracking

Applet running under the framework of fluent 3.0
随机推荐
SQLite3 data storage location created by Android
鼠标事件-事件对象
[SolidWorks] modify the drawing format
ANSI / nema- mw- 1000-2020 magnetic iron wire standard Latest original
Redux使用
php 获取音频时长等信息
The numerical value of the number of figures thought of by the real-time update of the ranking list
The circuit is shown in the figure, r1=2k Ω, r2=2k Ω, r3=4k Ω, rf=4k Ω. Find the expression of the relationship between output and input.
Summary of log feature selection (based on Tianchi competition)
很多小夥伴不太了解ORM框架的底層原理,這不,冰河帶你10分鐘手擼一個極簡版ORM框架(趕快收藏吧)
Graphic network: uncover the principle behind TCP's four waves, combined with the example of boyfriend and girlfriend breaking up, which is easy to understand
DataWorks值班表
#797div3 A---C
保姆级教程:Azkaban执行jar包(带测试样例及结果)
微软 AD 超基础入门
The method of using thread in PowerBuilder
Keras' deep learning practice -- gender classification based on inception V3
Apache多个组件漏洞公开(CVE-2022-32533/CVE-2022-33980/CVE-2021-37839)
【目标跟踪】|atom
Is NPDP recognized in China? Look at it and you'll see!