[LeetCode] Prime Palindrome 质数回文数
Find the smallest prime palindrome greater than or equal to `N`.
Recall that a number is prime if it's only divisors are 1 and itself, and it is greater than 1.
For example, 2,3,5,7,11 and 13 are primes.
Recall that a number is a palindrome if it reads the same from left to right as it does from right to left.
For example, 12321 is a palindrome.
Example 1:
Input: 6
Output: 7
Example 2:
Input: 8
Output: 11
Example 3:
Input: 13
Output: 101
Note:
1 <= N <= 10^8- The answer is guaranteed to exist and be less than
2 * 10^8.
这道题给了我们一个整数N,让找一个大于等于N的回文质数,既要求是质数,又要求是回文数。其实这道题可以当作两道题揉到了一起,判断质数和回文数分别可以当作单独的题。没想太多,博主上来就准备暴力搜索,先写两个子函数,分别用来判断质数和回文数,然后就直接从N开始搜索了,对每个数字都分别调用判断质数和回文数的子函数,若都满足,则返回该数即可。理想是丰满的,现实是骨感的,OJ 教你做人系列之 TLE 超时!想着优化一下吧,直接跳过所有的偶数吧(2除外),还是跪。看来小优化是行不通,得大改。
问题出现在哪里了呢?肯定是判断质数和回文数的子函数太占时间了,怎么优化呢?对于质数来说,非常的不规则,没有太好的办法来直接组成质数,而是需要通过验证来看其是否为质数。而回文数就不一样的,非常的有特点,我们可以直接按规律来组成回文数,而不是对每个数字都进行验证,这样的话就相当于去掉了验证回文数的步骤,是一个相当大的提升。怎么拼接呢?由于给了N的取值范围,我们可以遍历前一半的所有数字,然后翻转一下,组成后一半,两个拼起来就是回文数了。但问题又来了,回文数的长度是分奇偶的,长度为奇数的回文数,最中间的数字是没有对应的,肿么办?其实这道题挺考数学知识的,用到了一个比较偏门的定理,就是所有长度为偶数的回文数字一定是 11 的倍数。博主表示从没听过这个定理,证明过程请参见 lee215 大神的帖子。通过这个定理,可以知道除了11之外,所有长度为偶数的回文数都不是质数,那么当N为 [8, 11] 中的数字时,才会返回11,这个就当 corner cases 提前判断了,对于其他所有的都是符合规律的。那就可以只组奇数的回文数了,由于N的范围是 [1, 1e8],所以前一半范围是 [1, 1e5),因为还包含了最中间的那个数字,所以在翻转之后,记得要把第一位数字去掉,因为最中间的数字只能保留一个,然后把两个数字拼接起来。此时再判断这个拼接后的数字是否大于等N,并且是否是质数,都满足的话返回这个数字即可,参见代码如下:
class Solution {
public:
int primePalindrome(int N) {
if (N >= 8 && N <= 11) return 11;
for (int i = 1; i < 1e5; ++i) {
string s = to_string(i), t(s.rbegin(), s.rend());
int x = stoi(s + t.substr(1));
if (x >= N && isPrime(x)) return x;
}
return -1;
}
bool isPrime(int num) {
if (num < 2 || num % 2 == 0) return num == 2;
int limit = sqrt(num);
for (int i = 3; i <= limit; ++i) {
if (num % i == 0) return false;
}
return true;
}
};
Github 同步地址:
https://github.com/grandyang/leetcode/issues/866
类似题目:
参考资料:
https://leetcode.com/problems/prime-palindrome/
https://leetcode.com/problems/prime-palindrome/discuss/146798/Search-Palindrome-with-Odd-Digits
[LeetCode All in One 题目讲解汇总(持续更新中...)](https://www.cnblogs.com/grandyang/p/4606334.html)
[LeetCode] Prime Palindrome 质数回文数的更多相关文章
- leetcode 9 Palindrome Number 回文数
Determine whether an integer is a palindrome. Do this without extra space. click to show spoilers. S ...
- Leetcode 3——Palindrome Number(回文数)
Problem: Determine whether an integer is a palindrome. Do this without extra space. 简单的回文数,大一肯定有要求写过 ...
- [LeetCode]9. Palindrome Number回文数
Determine whether an integer is a palindrome. An integer is a palindrome when it reads the same back ...
- 【LeetCode】Palindrome Number(回文数)
这道题是LeetCode里的第9道题. 题目说的: 判断一个整数是否是回文数.回文数是指正序(从左向右)和倒序(从右向左)读都是一样的整数. 示例 1: 输入: 121 输出: true 示例 2: ...
- 4190. Prime Palindromes 一亿以内的质数回文数
Description The number 151 is a prime palindrome because it is both a prime number and a palindrome ...
- Leetcode(9)回文数
Leetcode(9)回文数 [题目表述]: 判断一个整数是否是回文数.回文数是指正序(从左向右)和倒序(从右向左)读都是一样的整数. 第一次:直接全部转 执行用时:148 ms: 内存消耗:13.4 ...
- 【LeetCode】9. Palindrome Number 回文数
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 公众号:负雪明烛 本文关键词:回文数,回文,题解,Leetcode, 力扣,Python ...
- LeetCode Problem 9:Palindrome Number回文数
描述:Determine whether an integer is a palindrome. Do this without extra space. Some hints: Could nega ...
- [LeetCode] Valid Palindrome 验证回文字符串
Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignori ...
随机推荐
- 找到多个与名为“Home”的控制器匹配的类型。解决方法
“/”应用程序中的服务器错误. 找到多个与名为“Home”的控制器匹配的类型.如果为此请求(“{controller}/{action}/{id}”)提供服务的路由没有指定命名空间以搜索与此请求相匹配 ...
- QT windeployqt
qt发布release版本时需要打包一些dll,需要哪些呢?请看截图: 在qt的安装包下找到这些文件,放在release文件夹下即可,当然有些时候也会需要一些其他的,比如含有串口的程序还需要加入Qt5 ...
- 如何用java实现一个p2p种子搜索(1)-概念
前言 说句大实话,网上介绍怎么用java实现p2p种子的搜索这种资料不是特别多,大部分都是python的,用python的话就会简单很多,它里面有很多简单方便的包,libtorrent等等,当然你用这 ...
- .Net core----mongodb在插入数据时,会产生时间差的问题
今天在给mongodb插入日期格式的数据时发现,日期时间相差8个小时,原来存储在mongodb中的时间是标准时间UTC +0:00,而中国的时区是+8.00 . 因此在插入的时候需要对时间进行处理: ...
- 【原创】大叔经验分享(51)docker报错Exited (137)
docker container启动失败,报错:Exited (137) *** ago,比如 Exited (137) 16 seconds ago 这时通过docker logs查不到任何日志,从 ...
- Redis从入门到精通【centos下的安装】
上传redis 到服务器 解压并重命名 然后yum -y install gcc-c++ zlib zlib-devel pcre pcre-devel openssl openssl-devel 然 ...
- 从头开始学JAVA[Day01]
1.Java程序的执行过程必须经过先编译,后解释两个步骤 Jvm的统一标准,具体定义了JVM的如下细节: --指令集 --寄存器 --类文件格式 --栈 --垃圾回收堆 --存储区 2.编译Java程 ...
- PHP获取汉字首字母并分组排序
<?php /** * 错误状态码定义 * User: xiucai * Date: 2018/3/11 * Time: 12:23 */ namespace extend; class Wor ...
- Oracle数据安全解决方案(1)——透明数据加密TDE
Oracle数据安全解决方案(1)——透明数据加密TDE2009年09月23日 22:49:00 华仔爱技术 阅读数:7991原文地址: http://www.oracle.com/technolog ...
- Android studio 常见错误修改
一,依赖冲突 All com.android.support libraries must use the exact same version specification (mixing versi ...