当前位置:网站首页>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);
}
}
边栏推荐
- Is NPDP recognized in China? Look at it and you'll see!
- 鼠标事件-事件对象
- See how names are added to namespace STD from cmath file
- [reinforcement learning medical] deep reinforcement learning for clinical decision support: a brief overview
- Keras' deep learning practice -- gender classification based on inception V3
- MySQL查询为什么没走索引?这篇文章带你全面解析
- Matlab r2021b installing libsvm
- Installing and using mpi4py
- Remote sensing contribution experience sharing
- Cross modal semantic association alignment retrieval - image text matching
猜你喜欢
Nacos microservice gateway component +swagger2 interface generation
咋吃都不胖的朋友,Nature告诉你原因:是基因突变了
Voice of users | winter goes and spring comes, waiting for flowers to bloom -- on gbase 8A learning comprehension
【目标跟踪】|DiMP: Learning Discriminative Model Prediction for Tracking
Introduction à l'outil nmap et aux commandes communes
神经网络与深度学习-5- 感知机-PyTorch
Nanny level tutorial: Azkaban executes jar package (with test samples and results)
很多小夥伴不太了解ORM框架的底層原理,這不,冰河帶你10分鐘手擼一個極簡版ORM框架(趕快收藏吧)
QT -- create QT program
burpsuite
随机推荐
The function of carbon brush slip ring in generator
Application of slip ring in direct drive motor rotor
Can you write the software test questions?
MySQL查询为什么没走索引?这篇文章带你全面解析
Ml self realization / logistic regression / binary classification
力扣6_1342. 将数字变成 0 的操作次数
Remote Sensing投稿經驗分享
Matlab r2021b installing libsvm
Introduction to ADB tools
PHP 计算个人所得税
快手小程序担保支付php源码封装
node js 保持长连接
阿南的判断
A comprehensive and detailed explanation of static routing configuration, a quick start guide to static routing
Analysis ideas after discovering that the on duty equipment is attacked
云原生应用开发之 gRPC 入门
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)
Exit of processes and threads
喜欢测特曼的阿洛
Flutter 3.0框架下的小程序运行