[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 ...
随机推荐
- 医学图像数据(三)——TCIA部分数据下载方式
前为止,本人还没有找到不需要账号的就可以部分下载的方式,因此这里讲的是需要注册账号下载部分数据的方法. 注意:下载部分数据需要注册账号 注册账号网址:https://public.cancerimag ...
- 3D Slicer中文教程(五)—三维视图颜色改变
3D Slicer在分割后三维重建的图像,效果很好,但是存在一定的不足,默认的颜色并不是很合适,这时手动设置三维视图下的需要的颜色就很有必要了.如下图所示,默认的三维重建后的颜色. 这样的颜色显然不是 ...
- mybatis调用oracle存储过程的几个参考例子
首先写一个存储过程: create or replace procedure p_syn_equipment_20161205 is sqlstr ); begin --清空表 sqlstr := ' ...
- 手把手教你写vue插件并发布(二)
前记:上一篇 https://www.cnblogs.com/adouwt/p/9211003.html, 说到了一个完整的vue插件开发.发布的流程,总结下来就讲了这么一个事,如何注入vue, 如果 ...
- 生成透视列之for xml path
临时表#t原始数据: 实现如下格式,即根据Province分组,把每个组对应的City列以某种格式展示: 实现方法: select t.Province,(select city+',' From # ...
- ngx_string()错误分析
#define ngx_string(str) { sizeof(str) - 1, (u_char) str } typedef struct { uint len; u_char* data; } ...
- python基础--absl.flags
之前在tensorflow的mnist例程中看到了使用 absl.flags的方法来载入和解析参数的,出于学习的目的,就自己试验了一下, 代码如下: # *_*coding:utf-8 *_* # a ...
- js逗号表达式
在js中的某些场景,","是一种运算符号,只不过他的优先级要低于普通的原酸符,在变量声明或者return中,经常看到逗号表达式. 声明变量: var a=1,b=2,c=3; co ...
- PADS Router VX.2.3 设置光标的类型
操作系统:Windows 10 x64 工具:PADS Router VX.2.3 菜单:Tools > Options...(快捷键:Ctrl + <Enter>) 在Option ...
- mysql把一个查询结果当作一个子集来查询
SELECT * FROM (SELECT * FROM table GROUP BY column HAVING COUNT(column)>=3 ORDER BY column DESC ...