当前位置:网站首页>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);
}
}
边栏推荐
- DataWorks值班表
- ClickHouse原理解析与应用实践》读书笔记(8)
- Urban land use distribution data / urban functional zoning distribution data / urban POI points of interest / vegetation type distribution
- Applet running under the framework of fluent 3.0
- 用户之声 | 冬去春来,静待花开 ——浅谈GBase 8a学习感悟
- body有8px的神秘边距
- In depth analysis of ArrayList source code, from the most basic capacity expansion principle, to the magic iterator and fast fail mechanism, you have everything you want!!!
- Js中forEach map无法跳出循环问题以及forEach会不会修改原数组
- 进程和线程的退出
- metasploit
猜你喜欢
Capability contribution three solutions of gbase were selected into the "financial information innovation ecological laboratory - financial information innovation solutions (the first batch)"
系统测试的类型有哪些,我给你介绍
Nanny level tutorial: Azkaban executes jar package (with test samples and results)
用户之声 | 冬去春来,静待花开 ——浅谈GBase 8a学习感悟
C语言-Cmake-CMakeLists.txt教程
分布式定时任务之XXL-JOB
Give some suggestions to friends who are just getting started or preparing to change careers as network engineers
List of top ten domestic industrial 3D visual guidance enterprises in 2022
Flutter 3.0框架下的小程序运行
进程和线程的退出
随机推荐
pb9.0 insert ole control 错误的修复工具
Optimization of ecological | Lake Warehouse Integration: gbase 8A MPP + xeos
Nmap tool introduction and common commands
从Starfish OS持续对SFO的通缩消耗,长远看SFO的价值
鼠标事件-事件对象
Give some suggestions to friends who are just getting started or preparing to change careers as network engineers
Mysql database (2)
【目标跟踪】|DiMP: Learning Discriminative Model Prediction for Tracking
Uniapp one click Copy function effect demo (finishing)
为什么更新了 DNS 记录不生效?
Dataworks duty table
If time is a river
Nacos microservice gateway component +swagger2 interface generation
分布式定时任务之XXL-JOB
SQLite3 data storage location created by Android
Many friends don't know the underlying principle of ORM framework very well. No, glacier will take you 10 minutes to hand roll a minimalist ORM framework (collect it quickly)
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.
nmap工具介紹及常用命令
QML fonts use pixelsize to adapt to the interface
什么样的MES系统才是好系统