LeetCode 866. Prime Palindrome
866. Prime Palindrome(回文素数)
题目:
求出大于或等于 N 的最小回文素数。
回顾一下,如果一个数大于 1,且其因数只有 1 和它自身,那么这个数是素数。
例如,2,3,5,7,11 以及 13 是素数。
回顾一下,如果一个数从左往右读与从右往左读是一样的,那么这个数是回文数。
例如,12321 是回文数。
思路:
思路还是挺清晰的,从给入数字向上检索,如果既是回文数,又是素数,就直接输出,如果不满足条件,那么就增加数字,继续判断。
这里有一个小问题,就是所有偶数位的回文数,都可以被11整除,至于证明。。。。。咱也不知道,咱也不敢问,所有如果发现这个数是偶数位,那么直接进一位,首数字和尾数字全为1,继续判断。
代码:
public static int primePalindrome(int N)
{
if (N <= 2)
return 2;
else if(N <= 3)
return 3;
else if(N <= 5)
return 5;
else if(N <= 7)
return 7;
else if(N <= 11)
return 11; for (int i = N; ; )
{
if(isHui(i) && isPrime(i))
return i; if((i + "").length() % 2 == 0)
i = (int)(Math.pow(10, (i + "").length()) + 1);
else
i++; }
} public static boolean isPrime(int i)
{
for (int j = 2; j <= Math.sqrt(i); j++)
if (i % j == 0)
return false;
return true;
} public static boolean isHui(int s)
{
String str = s+"";
int len = str.length();
for (int j = 0; j < len/2; j++)
if (str.charAt(j) != str.charAt(len-j-1))
return false;
return true;
}
LeetCode 866. Prime Palindrome的更多相关文章
- 866. Prime Palindrome
Find the smallest prime palindrome greater than or equal to N. Recall that a number is prime if it's ...
- [LeetCode] Prime Palindrome 质数回文数
Find the smallest prime palindrome greater than or equal to N. Recall that a number is prime if it's ...
- [Swift]LeetCode866. 回文素数 | Prime Palindrome
Find the smallest prime palindrome greater than or equal to N. Recall that a number is prime if it's ...
- Prime Palindrome Golf
Prime Palindrome Golf Do you know how to play Prime Palindrome Golf? You are given a number and your ...
- [LeetCode] 409. Longest Palindrome 最长回文
Given a string which consists of lowercase or uppercase letters, find the length of the longest pali ...
- [LeetCode] 680. Valid Palindrome II 验证回文字符串 II
Given a non-empty string s, you may delete at most one character. Judge whether you can make it a pa ...
- leetcode bug & 9. Palindrome Number
leetcode bug & 9. Palindrome Number bug shit bug "use strict"; /** * * @author xgqfrms ...
- Java for LeetCode 214 Shortest Palindrome
Given a string S, you are allowed to convert it to a palindrome by adding characters in front of it. ...
- [Leetcode][JAVA] Valid Palindrome
Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignori ...
随机推荐
- python 库 imgaug数据增强
安装及详细使用方法介绍: https://blog.csdn.net/qq_38451119/article/details/82428612 pip install imgaug 失败解决方法: 提 ...
- 全国数据json
{ "provinces": { "province": [ { "ssqid": "110000", "ss ...
- vue常用的修饰符
v-model修饰符 <template> <div id="demo14"> <p>-----------------模板语法之修饰符---- ...
- ylbtech-SQL-W3School-高级:SQL NOT NULL 约束
ylbtech-SQL-W3School-高级:SQL NOT NULL 约束 1.返回顶部 1. SQL NOT NULL 约束 NOT NULL 约束强制列不接受 NULL 值. NOT NULL ...
- nginx使用场景
1. 对外开放本地封闭Server 本地server无法对外开放,nginx做反向代理,对外开发,使得外部可以访问封闭服务. upstream npm { server ; keepalive ; } ...
- Python查询Mysql时返回字典结构的代码
Python查询Mysql时返回字典结构的代码 MySQLdb默认查询结果都是返回tuple,输出时候不是很方便,必须按照0,1这样读取,无意中在网上找到简单的修改方法,就是传递一个cursors.D ...
- LeetCode_13. Roman to Integer
13. Roman to Integer Easy Roman numerals are represented by seven different symbols: I, V, X, L, C, ...
- [译] NAT - 网络地址转换(2016)
[译] NAT - 网络地址转换(2016) Published at 2019-02-17 | Last Update 译者序 本文翻译自 2016 年的一篇英文博客 NAT - Network A ...
- 15-1 shell脚本进阶
shell脚本进阶 循环 循环执行 将某代码段重复运行多次 重复运行多少次 循环次数事先已知 循环次数事先未知 有进入条件和退出条件 for, while, until for循环 for VAR i ...
- JAVA计算字符串UTF-8字节数
String str = "C++/C#/JAVA软件开发"; try { System.out.println(str.getBytes("UTF-8").l ...