Ai
1 Star 2 Fork 5

LilithSangreal/LeetCode-Solutions

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
prime-palindrome.cpp 829 Bytes
一键复制 编辑 原始数据 按行查看 历史
kamyu 提交于 2018-09-16 20:36 +08:00 . Update prime-palindrome.cpp
// Time: O(n^(1/2) * (logn + n^(1/2)))
// Space: O(logn)
class Solution {
public:
int primePalindrome(int N) {
if (8 <= N && N <= 11) {
// any palindrome with even digits is multiple of 11
return 11;
}
for (int i = to_string(N).length() / 2; i < 100000; ++i) {
const string s = to_string(i), rev_s(s.rbegin(), s.rend());
int j = stoi(s + rev_s.substr(1));
if (j >= N && isPrime(j)) {
return j;
}
}
return -1;
}
private:
bool isPrime(int num) {
if (num < 2 || num % 2 == 0) {
return num == 2;
}
for (int i = 3; i * i <= num; i += 2) {
if (num % i == 0) {
return false;
}
}
return true;
}
};
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/LilithSangreal/LeetCode-Solutions.git
git@gitee.com:LilithSangreal/LeetCode-Solutions.git
LilithSangreal
LeetCode-Solutions
LeetCode-Solutions
master

搜索帮助