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 ...
随机推荐
- windows下安装node.js及环境配置、部署项目
windows下安装node.js及环境配置.部署项目 一.总结 一句话总结: 安装nodejs软件:就像普普通通的的软件那样安装 配置nodejs安装的全局模块的环境变量 并且安装cnpm(比如cn ...
- You can't specify target table 'a' for update in FROM clause
项目中有一个功能变动上线,其中有一张表ttt的字段cc,历史数据需要处理,把字段cc中为'xxx'的值替换为'yyy'. 表A结构如下: CREATE TABLE `ttt` ( `id` bigin ...
- MongoDB作为windows服务来安装-2
首先区官网下载对应版本的安装文件,我本地的环境是win7 bit64 我下载的版本是:mongodb-win32-x86_64-2.4.6 ok, 文件下载后,开始安装,这里要说一下,如果直接启动Mo ...
- iptables之精髓(二)
iptables实际操作 使用-v选项后,iptables为我们展示的信息更多了,那么,这些字段都是什么意思呢?我们来总结一下 pkts:对应规则匹配到的报文的个数. bytes:对应匹配到的报文包的 ...
- Python查询Mysql时返回字典结构的代码
Python查询Mysql时返回字典结构的代码 MySQLdb默认查询结果都是返回tuple,输出时候不是很方便,必须按照0,1这样读取,无意中在网上找到简单的修改方法,就是传递一个cursors.D ...
- php微信支付企业付款到零钱报错call faild, errorCode:58
这个报错一般是自己证书目录不是绝对目录,正确的目录结构应该是网站根目录下的:“C:\PHPWAMP_IN1\wwwroot\XXXX\XXXX.pem”. 还要注意的是文件夹命名一定 ...
- python 时间类型
- python进阶--多线程多进程
一.线程和进程 进程是拥有独立内存,能够独立运行的最小单位,也是程序执行的最小单位,线程是程序运行过程中,一个单一的顺序控制流程,是程序执行流的最小单位,一个进程至少包含一个线程,多线程共享进程的内存 ...
- 牛客竞赛(gcd,快速幂)
一.最大公约数和最小公倍数问题 题目描述: 输入2个正整数x0,y0(2<=x0<100000,2<=y0<=1000000),求出满足下列条件的P,Q的个数. 条件:1.P, ...
- NMS(Non-Maximum Suppression) 非极大值抑制
NMS 非极大值抑制:找到局部最大值,并删除邻域内其他的值. 简单说一下流程: 首先剔除背景(背景无需NMS),假设有6个边界框,根据分类置信度对这6个边界框做降序排列,假设顺序为A.B.C.D.E ...