当前位置:网站首页>leetcode 866. Prime Palindrome | 866. prime palindromes
leetcode 866. Prime Palindrome | 866. prime palindromes
2022-07-08 02:03:00 【Cold spring HQ】
subject
https://leetcode.com/problems/prime-palindrome/

Answer key
Refer to the answer :https://leetcode.com/problems/prime-palindrome/solution/
When you do it yourself , I split the odd palindrome string from the even palindrome string , Finally, return the minimum value between the two palindromes , I think it's more rigorous , Although this rigor can be mathematically proved to be unnecessary . Because if the digits are different , The size of the result does not increase monotonically , You shouldn't just return.
for example , Because if you count odd numbers first , If you count even numbers again , Odd numbers are one bit less than even numbers , In this way, it is possible to return the minimum value of error :
1002001 ( Suppose it is false)
10022001( Suppose it is false)
1003001 ( Suppose it is false)
10033001( Suppose it is true, Straight back , Is the minimum value of error )
1004001 ( Suppose it is true, This is the real minimum )
As for why the answer is written like this, there is no problem , Because All palindrome with even digits is multiple of 11. Equivalent to greater than 11 Even numbers of will return false, Will not cause the above example to return 10033001 The situation of .
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++) {
// Palindrome root Number of digits L
for (int root = (int) Math.pow(10, L - 1); root < Math.pow(10, L); root++) {
// Traversal digit is L All palindromes of root
// Even number palindrome string
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++) {
// Palindrome root Number of digits L
for (int root = (int) Math.pow(10, L - 1); root < Math.pow(10, L); root++) {
// Traversal digit is L All palindromes of root
// Odd digit palindrome string
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);
}
}

边栏推荐
- Codeforces Round #643 (Div. 2)——B. Young Explorers
- 阿锅鱼的大度
- What kind of MES system is a good system
- 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!!!
- Why does the updated DNS record not take effect?
- Analysis ideas after discovering that the on duty equipment is attacked
- Ml self realization /knn/ classification / weightlessness
- Beaucoup d'enfants ne savent pas grand - chose sur le principe sous - jacent du cadre orm, non, ice River vous emmène 10 minutes à la main "un cadre orm minimaliste" (collectionnez - le maintenant)
- JVM memory and garbage collection-3-object instantiation and memory layout
- XXL job of distributed timed tasks
猜你喜欢

How to make enterprise recruitment QR code?

COMSOL --- construction of micro resistance beam model --- final temperature distribution and deformation --- addition of materials

JVM memory and garbage collection-3-object instantiation and memory layout

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!!!

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

Nmap tool introduction and common commands

快手小程序担保支付php源码封装
![[recommendation system paper reading] recommendation simulation user feedback based on Reinforcement Learning](/img/48/3366df75c397269574e9666fcd02ec.jpg)
[recommendation system paper reading] recommendation simulation user feedback based on Reinforcement Learning

Reading notes of Clickhouse principle analysis and Application Practice (7)

Sword finger offer II 041 Average value of sliding window
随机推荐
nmap工具介紹及常用命令
Sum of submatrix
Usage of hydraulic rotary joint
Codeforces Round #643 (Div. 2)——B. Young Explorers
软件测试笔试题你会吗?
The numerical value of the number of figures thought of by the real-time update of the ranking list
[target tracking] |atom
Installing and using mpi4py
VIM use
Redismission source code analysis
阿锅鱼的大度
SQLite3 data storage location created by Android
Mouse event - event object
如何用Diffusion models做interpolation插值任务?——原理解析和代码实战
metasploit
Why did MySQL query not go to the index? This article will give you a comprehensive analysis
MySQL查询为什么没走索引?这篇文章带你全面解析
C language -cmake cmakelists Txt tutorial
QT -- create QT program
CV2 read video - and save image or video