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 ...
随机推荐
- 【java中的final关键字】
转自:https://www.cnblogs.com/xiaoxi/p/6392154.html 一.final关键字的基本用法 在Java中,final关键字可以用来修饰类.方法和变量(包括成员变量 ...
- 定位ScheduledExecutorService过了一段时间不执行问题
今天查看生产环境的sentinel控制台,发现某dubbo应用一共5个节点,有3个失联了. 查看失联节点的应用日志,服务没有挂,各dubbo接口的日志正常在打印. 在应用节点ping/telnet s ...
- kotlin之MutableMap委托
fun main(arg: Array<String>) { val map = mutableMapOf("name" to "tom", ) v ...
- js取url问号后的参数方法封装
工具方法: function getRequest() { var url = location.search; // 获取url中?后面的字符串 var theRequest = new Objec ...
- 【UI】数据表格设计
https://www.smashingmagazine.com/2019/02/complex-web-tables/ https://www.smashingmagazine.com/2019/0 ...
- laravel5.1设置cookie
Laravel 所建立的 cookie 会加密并且加上认证记号,这代表着被用户擅自更改的 cookie 会失效.从请求中取得Cookie值,你使用cookie方法 $value = $request- ...
- Install Virtualbox on CentOS7---(後話,最終還是沒有用virtualbox做VM server ,感覺只適用于桌面)
參考: https://wiki.centos.org/zh-tw/HowTos/Virtualization/VirtualBox cd /etc/yum.repos.d wget http://d ...
- What is Dark Social & Dark Traffic?
What is Dark Social & Dark Traffic? By Iaroslav Kudritskiy Google Analytics is supposed to speak ...
- WIN10家庭版添加"本地安全策略"
新建文本文件 输入一下命令 @echo off pushd "%~dp0" dir /b C:\Windows\servicing\Packages\Microsoft-Windo ...
- Webpack实现按需打包Lodash的几种方法详解
参考链接:https://www.jb51.net/article/113235.htm 在vue-cli3中需要将babel-preset-es2015插件更换为@babel/preset-env插 ...